链接:https://leetcode-cn.com/problems/search-a-2d-matrix-ii
本题的解题思路是Leetcode378. 有序矩阵中第K小的元素的解题思路的一部分
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size() == 0) return false;
int h = matrix.size(), w = matrix[0].size(); // 从左下角开始向上寻找,每次去除一行或者去除一列
int i = h - 1, j = 0;
while(i >= 0 && j < w){
if (matrix[i][j] < target){
++j;
}else if(matrix[i][j] > target){
--i;
}
else return true;
}
return false;
}
};