240. 搜索二维矩阵 II
# 240. 搜索二维矩阵 II (opens new window)
# 1.抽象二叉搜索树
分析:站在矩阵右上角进行搜索。左边的元素一定小,下面的元素一定大。
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int x=0,y=matrix[0].length-1;
while(true){
if(target<matrix[x][y]){
if(y-1<0) return false;
y--;
}
else if(target>matrix[x][y]){
if(x+1>=matrix.length) return false;
x++;
}
else{
return true;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
编辑 (opens new window)
上次更新: 2023/12/15, 15:49:57