File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments