二维数组查找问题

来源:互联网 发布:linux查找文件名称 编辑:程序博客网 时间:2024/04/30 03:43

题目:在一个二维数组中,每行都按照从左至右递增的顺序排序,每一列都按照从上到下递增顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有这个整数。这个例子来自剑指offer
最容易想到就是逐个遍历矩阵,但是没有用到题干每一列和每一行都递增的信息。通过观察,从矩阵右上角或左下角开始遍历,这样能缩短每次遍历规模。例如,输入图中矩阵和整数7,则从右上角开始查找,9大于7,显然9所在的列不用查找,则9所在行则需要继续查找,由于9已经比较过,所以删除9所在这一列。则需要查找数组为
删掉9所在的列之后剩余数组
7在与8比较,由于8大于7,同理,删掉8所在列。
删掉8之后剩余数组
7与2比较,大于2,于是删掉2所在行。
这里写图片描述
7月4比较,大于4,删掉4所在行。
这里写图片描述
7月7比较相等,于是返回真,说明要查找的数在矩阵中。

#include<stdio.h>#include<iostream>using namespace std;bool find(int *matrix,int rows,int columns,int num){    bool found = false;    if (matrix != NULL && rows >0 && columns > 0){        int row = 0;        int column = columns-1;        while(row < rows && column >= 0){            if (matrix[row * columns + column] == num){                found=true;                break;            }            else if(matrix[row * columns + column] > num)                column--;            else                row++;        }    }    return found;}int main(){    int matrix[][4]={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};    int *p=(int *)matrix;    cout<<find(p,4,4,7)<<endl;    return 0;

上面代码是自己写的,下面这个代码是摘自<<剑指offer>>,版权归作者所有,作者写的代码相当有参考价值,特别是测试用例的编写部分,好好学习。

#include <iostream>// 二维数组matrix中,每一行都从左到右递增排序,// 每一列都从上到下递增排序bool Find(int* matrix, int rows, int columns, int number){    bool found = false;    if(matrix != NULL && rows > 0 && columns > 0)    {        int row = 0;        int column = columns - 1;        while(row < rows && column >=0)        {            if(matrix[row * columns + column] == number)            {                found = true;                break;            }            else if(matrix[row * columns + column] > number)                -- column;            else                ++ row;        }    }    return found;}// ====================测试代码====================void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected){    if(testName != NULL)        printf("%s begins: ", testName);    bool result = Find(matrix, rows, columns, number);    if(result == expected)        printf("Passed.\n");    else        printf("Failed.\n");}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数在数组中void Test1(){    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};    Test("Test1", (int*)matrix, 4, 4, 7, true);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数不在数组中void Test2(){    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};    Test("Test2", (int*)matrix, 4, 4, 5, false);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数是数组中最小的数字void Test3(){    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};    Test("Test3", (int*)matrix, 4, 4, 1, true);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数是数组中最大的数字void Test4(){    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};    Test("Test4", (int*)matrix, 4, 4, 15, true);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数比数组中最小的数字还小void Test5(){    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};    Test("Test5", (int*)matrix, 4, 4, 0, false);}//  1   2   8   9//  2   4   9   12//  4   7   10  13//  6   8   11  15// 要查找的数比数组中最大的数字还大void Test6(){    int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};    Test("Test6", (int*)matrix, 4, 4, 16, false);}// 鲁棒性测试,输入空指针void Test7(){    Test("Test7", NULL, 0, 0, 16, false);}int main(){    Test1();    Test2();    Test3();    Test4();    Test5();    Test6();    Test7();    return 0;}
0 0
原创粉丝点击