-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (25 loc) · 871 Bytes
/
Solution.java
File metadata and controls
31 lines (25 loc) · 871 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
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
return false;
int rowIdx = 0;
int colsNum = matrix[0].length;
int rowsNum = matrix.length;
while (rowIdx < rowsNum && matrix[rowIdx][colsNum - 1] < target)
rowIdx++;
if (rowIdx == rowsNum)
return false;
int left = 0;
int right = colsNum - 1;
while (left <= right) {
int mid = (left + right) >> 1;
if (matrix[rowIdx][mid] < target)
left = mid + 1;
else if (matrix[rowIdx][mid] > target)
right = mid - 1;
else
return true;
}
return false;
}
}