-
Notifications
You must be signed in to change notification settings - Fork 43
/
SearchA2DMatrix.java
74 lines (63 loc) · 2.01 KB
/
SearchA2DMatrix.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package LeetCodeJava.BinarySearch;
// https://leetcode.com/problems/search-a-2d-matrix/
public class SearchA2DMatrix {
// V0
// IDEA : BINARY SEARCH + FLATTEN MATRIX
public boolean searchMatrix(int[][] matrix, int target) {
int length = matrix[0].length;
int width = matrix.length;
// flatten matrix + binary search
/** NOTE !!! we FLATTEN matrix as 1D array, and do binary search */
int l = 0;
int r = length * width - 1;
while (r >= l){
int mid = (l + r) / 2;
/** NOTE !!!
* -> whether x or y axis
* -> we do with length
* -> e.g. y = mid / length
* -> x = mid % length
*/
int cur = matrix[mid / length][mid % length];
if (cur == target){
return true;
}else if (cur < target){
l = mid + 1;
}else{
r = mid - 1;
}
}
return false;
}
// V1
// IDEA : BINARY SEARCH + FLATTEN MATRIX
// https://leetcode.com/problems/search-a-2d-matrix/editorial/
public boolean searchMatrix_2(int[][] matrix, int target) {
int m = matrix.length;
if (m == 0)
return false;
int n = matrix[0].length;
// binary search
/** NOTE !!! FLATTEN MATRIX */
int left = 0, right = m * n - 1;
int pivotIdx, pivotElement;
while (left <= right) {
pivotIdx = (left + right) / 2;
/** NOTE !!! TRICK HERE :
*
* pivotIdx / n : y index
* pivotIdx % n : x index
*/
pivotElement = matrix[pivotIdx / n][pivotIdx % n];
if (target == pivotElement)
return true;
else {
if (target < pivotElement)
right = pivotIdx - 1;
else
left = pivotIdx + 1;
}
}
return false;
}
}