-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch-2d-matrix.java
More file actions
65 lines (61 loc) · 1.99 KB
/
search-2d-matrix.java
File metadata and controls
65 lines (61 loc) · 1.99 KB
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
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int low = 0, high = matrix.length-1;
while (low<=high) {
int mid = low + (high - low) / 2;
if (matrix[mid][0] == target) return true;
if (matrix[mid][0] < target) low = mid+1;
else high = mid-1;
}
if(high<0) return false;
int row=high;
low=0;high=matrix[0].length-1;
while (low<=high) {
int mid = low + (high - low) / 2;
if (matrix[row][mid] == target) return true;
if (matrix[row][mid] < target) low = mid+1;
else high = mid-1;
}
return false;
}
}
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int r=matrix.length-1,c=matrix[0].length-1,lr=0,lc=0,col=c;
while(lr<=r&&lc<=c) {
int mr=lr+(r-lr)/2;
int mc=lc+(c-lc)/2;
int val=matrix[mr][mc];
if(target<val&&target>matrix[mr][0]) {
if(bs(matrix,target,1,mc-1,mr)) return true;
return false;
}
else if(target<val&&target<matrix[mr][0]) {
r=mr-1;
}
else if(target>val&&target<matrix[mr][col]) {
if(bs(matrix,target,mc+1,col-1,mr)) return true;
return false;
}
else if(target>val&&target>matrix[mr][col]) {
lr=mr+1;
}
else return true;
}
return false;
}
public boolean bs(int[][] m,int t,int l,int h,int row) {
while(l<=h) {
int mid=l+(h-l)/2;
int val=m[row][mid];
if(t<val) h=mid-1;
else if(t>val) l=mid+1;
else return true;
}
return false;
}
}