forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch2DMatrix.java
More file actions
23 lines (22 loc) · 662 Bytes
/
Search2DMatrix.java
File metadata and controls
23 lines (22 loc) · 662 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Search2DMatrix {
// 耗时11ms
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0) {
return false;
}
int row = matrix.length, col = matrix[0].length;
int left = 0, right = row * col - 1;
while (left <= right) {
int mid = (left + right) / 2;
int x = mid / col, y = mid % col;
if (target == matrix[x][y]) {
return true;
} else if (target > matrix[x][y]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
}