剑指OFFER之二维数组中的查找

题目描述:

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

输入:

输入可能包含多个测试样例,对于每个测试案例,

输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的矩阵的行数和列数。

输入的第二行包括一个整数t(1<=t<=1000000):代表要查找的数字。

接下来的m行,每行有n个数,代表题目所给出的m行n列的矩阵(矩阵如题目描述所示,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。

输出:

对应每个测试案例,

输出”Yes”代表在二维数组中找到了数字t。

输出”No”代表在二维数组中没有找到数字t。

样例输入:
3 3
5
1 2 3
4 5 6
7 8 9
3 3
1
2 3 4
5 6 7
8 9 10
3 3
12
2 3 4
5 6 7
8 9 10
样例输出:
Yes
No
No

Code:
#include <stdio.h>
 
bool Find(int *matrix,int row,int column,int key){
    bool found=false;
    int x=0,y=column-1;
    while(x<row&&y>=0){
        if(matrix[x*column+y]==key){
            found=true;
            break;
        }else{
            if(matrix[x*column+y]>key){
                --y;
            }else{
                ++x;
            }
        }
    }
    return found;
}
 
int matrix[1000000];
 
int main()
{
    int n,m,val;
    while(scanf("%d%d",&n,&m)!=EOF){
        scanf("%d",&val);
        for(int i=0;i<n*m;++i){
            scanf("%d",&matrix[i]);
        }
        bool ans=Find(matrix,n,m,val);
        if(ans==true){
            printf("Yes
");
        }else{
            printf("No
");
        }
    }
    return 0;
}
 
/**************************************************************
    Problem: 1384
    User: lcyvino
    Language: C
    Result: Accepted
    Time:670 ms
    Memory:5424 kb
****************************************************************/
原文地址:https://www.cnblogs.com/Murcielago/p/4148760.html