-
Notifications
You must be signed in to change notification settings - Fork 0
/
search-a-2d-matrix.java
50 lines (37 loc) · 1.37 KB
/
search-a-2d-matrix.java
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
42
43
44
45
46
47
48
49
50
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
{
return false;
}
/* -- 12 ms runtime
else{
for(int i = 0 ; i < matrix.length ; i++){
int col_length = matrix[i].length;
if(target >= matrix[i][0] && target <= matrix[i][col_length-1]){
int[] arr = matrix[i];
if( Arrays.binarySearch(arr, target) >=0){
return true;
}
}
}
} */
for(int i = 0 ; i < matrix.length ; i++){
int col_length = matrix[i].length;
if(target > matrix[i][col_length -1])
continue;
if(target >= matrix[i][0]){
int stop = 0;
while(col_length>=1 && stop == 0){
if(matrix[i][col_length -1] == target)
{
stop = 1;
return true;
}
col_length--;
}
}
}
return false;
}
}