-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSearchA2DMatrix.java
More file actions
30 lines (29 loc) · 851 Bytes
/
SearchA2DMatrix.java
File metadata and controls
30 lines (29 loc) · 851 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
28
29
30
public class SearchA2DMatrix {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0) return false;
int m = matrix.length;
int n = matrix[0].length;
int start = 0;
int end = m * n - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
int[] xy = coords(mid, m, n);
int x = xy[0];
int y = xy[1];
if (matrix[x][y] < target) {
start = mid + 1;
} else if (matrix[x][y] > target) {
end = mid - 1;
} else {
return true;
}
}
return false;
}
public int[] coords(int index, int m, int n) {
int[] xy = new int[2];
xy[0] = index / n;
xy[1] = index % n;
return xy;
}
}