Skip to content

Commit c22efba

Browse files
authored
Create 74. Search a 2D matrix
1 parent 580b384 commit c22efba

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

74. Search a 2D matrix

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public boolean searchMatrix(int[][] matrix, int target) {
3+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
4+
return false;
5+
}
6+
int row = matrix.length;
7+
int col = matrix[0].length;
8+
for(int i = 0; i < row; i++){
9+
if(matrix[i][0] == target){
10+
return true;
11+
}else if(matrix[i][0] < target && i + 1 < row && matrix[i + 1][0] > target){
12+
for(int j = 1; j < col; j++){
13+
if(matrix[i][j] == target){
14+
return true;
15+
}else if(matrix[i][j] > target){
16+
return false;
17+
}
18+
}
19+
}
20+
// judge the final line
21+
if(i == row - 1){
22+
for(int j = 1; j < col; j++){
23+
if(matrix[i][j] == target){
24+
return true;
25+
}else if(matrix[i][j] > target){
26+
return false;
27+
}
28+
}
29+
}
30+
}
31+
return false;
32+
}
33+
}

0 commit comments

Comments
 (0)