-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchA2DMatrix.java
More file actions
27 lines (25 loc) · 820 Bytes
/
SearchA2DMatrix.java
File metadata and controls
27 lines (25 loc) · 820 Bytes
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
//O(log(m*n)) O(1)
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
int rs = 0;
int re = matrix.length;
while(rs < re) {
int mid = (rs + re) / 2;
if(matrix[mid][0] == target) return true;
else if(matrix[mid][0] > target) re = mid;
else rs = mid + 1;
}
int r = rs - 1;
if(r < 0) return false;
int cs = 0;
int ce = matrix[r].length;
while(cs < ce) {
int mid = (cs + ce) / 2;
if(matrix[r][mid] == target) return true;
else if(matrix[r][mid] > target) ce = mid;
else cs = mid + 1;
}
return false;
}
}