-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixSearcher2.java
57 lines (48 loc) · 1.69 KB
/
MatrixSearcher2.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
49
50
51
52
53
54
55
56
57
package org.sean.array;
/** * 240. Search a 2D Matrix II */
public class MatrixSearcher2 {
public boolean searchMatrix(int[][] matrix, int target) {
// S1: O(m*n)
// S2: lgN*N
if (matrix == null || matrix.length == 0) return false;
if (matrix.length == 1 && matrix[0].length == 1) return matrix[0][0] == target;
int row = matrix.length;
int col = matrix[0].length;
if (col == 0) return false;
if (col >= row) {
for (int i = 0; i < row; i++) {
if (matrix[i][0] > target || matrix[i][col - 1] < target) continue;
boolean found = binSearch(matrix, target, true, i);
if (found) return true;
}
} else {
for (int i = 0; i < col; i++) {
if (matrix[0][i] > target || matrix[row - 1][i] < target) continue;
boolean found = binSearch(matrix, target, false, i);
if (found) return true;
}
}
return false;
}
private boolean binSearch(int[][] array, int target, boolean byRow, int index) {
int left = 0;
int right = byRow ? array[0].length - 1 : array.length - 1; // column : row
int mid;
while (left <= right) {
mid = (left + right) / 2;
int elem; // = array[mid];
if (byRow) {
elem = array[index][mid];
} else {
elem = array[mid][index];
}
if (elem == target) return true;
else if (elem < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
}