[leetcode]Search a 2D Matrix

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(matrix.empty()) return false;
        
        int M = matrix.size();
        int N = matrix[0].size();
        
        int i = 0, j = N-1;
        
        while( i < M && j >=0){
            if(matrix[i][j] == target) return true;
            else if(matrix[i][j] < target) i++;
            else j--;
        }
        return false;
        
    }
};


你可能感兴趣的:(LeetCode)