240. 搜索二维矩阵 II
方法一 z字形查找
从矩阵的右上角开始遍历,
- 如果相等,返回true
- 小于target,说明需要更大,row++;
- 大于target,说明需要更小,col–;
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row = matrix.size(),col = matrix[0].size();
int curRow = 0, curCol = col-1;
while(curRow<row && curCol>=0){
if(matrix[curRow][curCol] == target){
return true;
}else if(matrix[curRow][curCol] > target){
curCol--;
}else if(matrix[curRow][curCol] < target){
curRow++;
}
}
return false;
}
};