-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path74. Search a 2D matrix
33 lines (33 loc) · 1.09 KB
/
74. Search a 2D matrix
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
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return false;
}
int row = matrix.length;
int col = matrix[0].length;
for(int i = 0; i < row; i++){
if(matrix[i][0] == target){
return true;
}else if(matrix[i][0] < target && i + 1 < row && matrix[i + 1][0] > target){
for(int j = 1; j < col; j++){
if(matrix[i][j] == target){
return true;
}else if(matrix[i][j] > target){
return false;
}
}
}
// judge the final line
if(i == row - 1){
for(int j = 1; j < col; j++){
if(matrix[i][j] == target){
return true;
}else if(matrix[i][j] > target){
return false;
}
}
}
}
return false;
}
}