forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch a 2D Matrix II.java
34 lines (34 loc) · 962 Bytes
/
Search a 2D Matrix II.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
// Runtime: 14 ms (Top 30.54%) | Memory: 57.2 MB (Top 69.94%)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;
int lo = 0, hi = rows;
while(lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
if (matrix[mid][0] <= target) {
lo = mid;
} else {
hi = mid;
}
}
int[] prospect;
for (int i = 0; i <= lo; i++) {
prospect = matrix[i];
int l = 0;
int h = cols;
while (l + 1 < h) {
int mid = l + (h - l) / 2;
if (prospect[mid] <= target) {
l = mid;
} else {
h = mid;
}
}
if (prospect[l] == target) {
return true;
}
}
return false;
}
}