-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
41 lines (38 loc) · 1.07 KB
/
Solution.java
File metadata and controls
41 lines (38 loc) · 1.07 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
38
39
40
41
package leetcode._74_;
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
int rows = matrix.length;
int width = matrix[0].length;
if (width == 0) {
return false;
}
int targetRowIndex = -1;
for (int i = 0; i < rows; i++) {
if (matrix[i][0] <= target && matrix[i][width - 1] >= target) {
targetRowIndex = i;
break;
}
}
if (targetRowIndex < 0) {
return false;
}
int[] targetRow = matrix[targetRowIndex];
int left = 0;
int right = width;
int medium;
while (left < right) {
medium = (left + right) / 2;
if (targetRow[medium] == target) {
return true;
} else if (targetRow[medium] > target) {
right = medium;
} else {
left = medium + 1;
}
}
return false;
}
}