-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchMatrix.java
More file actions
33 lines (32 loc) · 919 Bytes
/
SearchMatrix.java
File metadata and controls
33 lines (32 loc) · 919 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
31
32
33
public class SearchMatrix {
public boolean searchMatrix(int[][] matrix, int x) {
int rows = matrix.length;
int cols = matrix[0].length;
int low = 0, high = rows* cols -1;
while(low <= high){
int mid = (low + high) / 2;
int val = matrix[mid / cols][mid % cols];
if(val == x) return true;
if(val > x){
high = mid -1;
}else{
low = mid +1;
}
}
return false;
}
/*
public boolean searchMatrix(int[][] matrix, int target) {
int i = 0, j = matrix[0].length -1;
while(i < matrix.length && i >=0 && j >=0 && j <= matrix[0].length){
if(matrix[i][j] == target) return true;
if(matrix[i][j] > target){
j--;
}else{
i++;
}
}
return false;
}
*/
}