-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search2DMatrix.java
37 lines (37 loc) · 1.05 KB
/
Search2DMatrix.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
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
if (m == 0)
return false;
int n = matrix[0].length;
if (n == 0)
return false;
int start = 0, end = m - 1;
int[] found = null;
while (start <= end) {
int mid = start + (end - start) / 2;
if (matrix[mid][0] <= target && matrix[mid][n - 1] >= target) {
found = matrix[mid];
break;
}
else if (matrix[mid][0] > target)
end = mid - 1;
else
start = mid + 1;
}
if (found == null)
return false;
start = 0;
end = n - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (found[mid] == target)
return true;
else if (found[mid] < target)
start = mid + 1;
else
end = mid - 1;
}
return false;
}
}