-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path028. 搜索二维矩阵.java
46 lines (46 loc) · 1.01 KB
/
028. 搜索二维矩阵.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Solution
{
public boolean searchMatrix(int[][] matrix, int target)
{
int hang = matrix.length;
if(hang==0)
return false;
int lie = matrix[0].length;
if(lie==0)
return false;
int x = 0;
int y = hang;
int m;
while(x<y)
{
m = x+(y-x)/2;
if(matrix[m][lie-1]<target)
x=m+1;
else
y=m;
}
if(x>=hang)
return false;
if(matrix[x][lie-1]==target)
return true;
else
{
int hang_yes=x;
x=0;
y=lie;
while(x<y)
{
m=x+(y-x)/2;
if(matrix[hang_yes][m]<target)
x=m+1;
else
y=m;
}
if(x>=lie)
return false;
if(matrix[hang_yes][x]==target)
return true;
}
return false;
}
}