class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length, n = matrix[0].length;
int r = 0, c = n - 1;
while (r < m && c >= 0) {
if (matrix[r][c] < target) r++;
else if (matrix[r][c] > target) c--;
else return true;
}
return false;
}
}
将矩阵转换为BST(二叉搜索树),起始点是右上角(左下角也可以)
来源于三叶
【宫水三叶】一题双解 :「二分」&「抽象 BST」 - 搜索二维矩阵 II - 力扣(LeetCode) (leetcode-cn.com)