-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc74.java
More file actions
33 lines (30 loc) · 994 Bytes
/
Lc74.java
File metadata and controls
33 lines (30 loc) · 994 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
package leetcode;
/**
* @author Kuma
* @date 2021年3月30日
* 74.搜索二维矩阵
*/
public class Lc74 {
public static boolean searchMatrix(int[][] matrix, int target) {
return binarySearch(matrix, target, 0 , matrix.length * matrix[0].length - 1);
}
public static boolean binarySearch(int[][] matrix, int target, int left, int right){
if (left <= right){
int mid = (left + right) / 2;
int i = mid / matrix[0].length;
int j = mid % matrix[0].length;
if (matrix[i][j] > target){
return binarySearch(matrix, target, left, mid - 1);
}else if (matrix[i][j] < target){
return binarySearch(matrix, target, mid + 1, right);
}else {
return true;
}
}
return false;
}
public static void main(String[] args) {
int[][] arr = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
searchMatrix(arr,3);
}
}