-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT28.java
More file actions
24 lines (24 loc) · 720 Bytes
/
T28.java
File metadata and controls
24 lines (24 loc) · 720 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
public class T28 {
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
if(matrix == null || matrix.length == 0 || matrix[0].length ==0)
return false;
int m = matrix.length;
int n = matrix[0].length;
int left = 0;
int right = m * n - 1;
int middle = 0;
while(left <= right) {
middle = (right + left)/2;
int tmp = matrix[middle/n][middle%n];
if (tmp < target) {
left = middle + 1;
} else if (tmp > target) {
right = middle - 1;
} else {
return true;
}
}
return false;
}
}