-
Notifications
You must be signed in to change notification settings - Fork 0
/
Searcha2DMatrix.java
74 lines (56 loc) · 2.34 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
/*
*Key point: binary search to locate the row. It doesn't look like
* typical binary search because we need to confirm a range
* rather than exactly the element. Refer to Line 45
*/
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
//if target is out of scope, return false
if(target < matrix[0][0] || target > matrix[m-1][n-1])
return false;
int row = 0;
//if there's more than one row, binary search which row is the target in
if(m > 1)
row = binarySearchRow(matrix, target, 0, m-1);
//after locating the row, binary search within the row
boolean result = binarySearchIndex(matrix, target, 0, n-1, row);
return result;
}
//binary search among the first elements of each row,
//to locate which row is the target in
public int binarySearchRow(int[][] matrix, int target, int begin, int end){
int mid = (begin + end)/2;
int row = mid;
if(begin < end){
//recursive search on first part
if(matrix[mid][0] > target){
row = binarySearchRow(matrix, target, begin, mid-1);
}
else if(matrix[mid][0] < target){
//checking whether target is not in row matrix[mid]
if(matrix[mid+1][0] <= target){//don't forget the '=' condition
row = binarySearchRow(matrix, target, mid+1, end);
}
else //target is in row matrix[mid]
return mid;
}
}
return row;
}
//binary search within a row
public boolean binarySearchIndex(int[][] matrix, int target, int begin, int end, int row){
int mid = (begin + end)/2;
boolean result = false;
if(begin <= end){
if(matrix[row][mid] > target)
result = binarySearchIndex(matrix, target, begin, mid-1, row);
else if(matrix[row][mid] < target)
result = binarySearchIndex(matrix, target, mid+1, end, row);
else
result = true;
}
return result;
}
}