-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode74.java
More file actions
37 lines (34 loc) · 1.19 KB
/
LeetCode74.java
File metadata and controls
37 lines (34 loc) · 1.19 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
public class LeetCode74 {
public static void main(String[] args) {
// 输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
// 输出:true
System.out.println(new Solution74()
.searchMatrix(new int[][] { { 1, 3, 5, 7 }, { 10, 11, 16, 20 }, { 23, 30, 34, 60 } }, 3));
// 输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
// 输出:false
System.out.println(new Solution74()
.searchMatrix(new int[][] { { 1, 3, 5, 7 }, { 10, 11, 16, 20 }, { 23, 30, 34, 60 } }, 13));
}
}
class Solution74 {
public boolean searchMatrix(int[][] matrix, int target) {
int low = 0;
int high = matrix.length * matrix[0].length - 1;
int mid;
int row;
int col;
while (low <= high) {
mid = (low + high) / 2;
row = mid / matrix[0].length;
col = mid % matrix[0].length;
if (matrix[row][col] < target) {
low = mid + 1;
} else if (matrix[row][col] > target) {
high = mid - 1;
} else {
return true;
}
}
return false;
}
}