-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (36 loc) · 1 KB
/
Solution.java
File metadata and controls
39 lines (36 loc) · 1 KB
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
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = m == 0 ? 0 : matrix[0].length;
if (m == 0 || n == 0)
return false;
int low = 0,
high = m - 1,
rowIndex = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (matrix[mid][n - 1] < target)
low = mid + 1;
else if (matrix[mid][0] > target)
high = mid - 1;
else {
rowIndex = mid;
break;
}
}
if (rowIndex == -1)
return false;
low = 0;
high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (matrix[rowIndex][mid] < target)
low = mid + 1;
else if (matrix[rowIndex][mid] > target)
high = mid - 1;
else
return true;
}
return false;
}
}