forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0074-search-a-2d-matrix.java
41 lines (35 loc) · 1.05 KB
/
0074-search-a-2d-matrix.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
//Similar to binary search in 1D array, here the search space is from (0,0) to (m-1,n-1)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int i = 0, j = matrix[0].length - 1;
while (i < matrix.length && j >= 0) {
if (matrix[i][j] == target) {
return true;
} else if (matrix[i][j] > target) {
j--;
} else {
i++;
}
}
return false;
}
// Time: O(log(mn)) | Space: O(1)
public boolean searchMatrix2(int[][] matrix, int target) {
if(matrix.length == 0) return false;
int rows = matrix.length;
int columns = matrix[0].length;
int low = 0;
int high = rows * columns;
while(low < high) {
int mid = (low+high)/2;
if(matrix[mid/columns][mid%columns] == target) {
return true;
} else if (matrix[mid/columns][mid%columns] < target) {
low = mid+1;
} else {
high = mid;
}
}
return false;
}
}