-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
48 lines (48 loc) · 1.35 KB
/
Solution.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
47
48
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int begin=0;
int end=matrix.length-1;
int t=end/2;
if (target<matrix[0][0]||target>matrix[matrix.length-1][matrix[matrix.length-1].length-1]) {
return false;
}
while (end-begin>1) {
if (matrix[t][0]==target) {
return true;
}else if (matrix[end][0]<target) {
t=end;
begin=end;
break;
}else if (matrix[t][0]>target) {
end=t;
t=(begin+end)/2;
}else {
begin=t;
t=(begin+end)/2;
}
}
if (matrix[t][0]==target||matrix[begin][0]==target||matrix[end][0]==target) {
return true;
}else if (matrix[t][0]>target) {
t=t-1;
}
int b=0;
int e=matrix[t].length-1;
int tt=(b+e)/2;
while (e-b>1) {
if (matrix[t][tt]==target) {
return true;
}else if (matrix[t][tt]>target) {
e=tt;
tt=(b+e)/2;
}else {
b=tt;
tt=(b+e)/2;
}
}
if (matrix[t][b]==target||matrix[t][e]==target) {
return true;
}
return false;
}
}