Sword Finger Offer Interview Question 04. Find in a two-dimensional array [Simple]

answer:

I thought about how to achieve it for a long time. Starting from the upper left corner, I found it too troublesome.

It turns out that the bottom right corner is so simple

class Solution {
public:
    bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
        if(matrix.size()==0)    return false;
        int m=matrix.size()-1;
        int n=matrix[0].size()-1;
        int i=m,j=0;
        while(i>=0&&j<=n){
            if(matrix[i][j]==target)    return true;
            if(matrix[i][j]>target) i--;
            else if(matrix[i][j]<target)    j++;
        }
        return false;
    }
};

Published 65 original articles · Like1 · Visits 476

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105566772