-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc_240.java
More file actions
38 lines (28 loc) · 761 Bytes
/
Lc_240.java
File metadata and controls
38 lines (28 loc) · 761 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
32
33
34
35
36
37
38
public class Lc_240 {
public static void main(String[] args) {
int[][] matrix = {
{1,5,7,9},
{2,6,8,10},
{3,11,12,13},
{4,14,15,16}
};
int target = 122;
System.out.println(searchMatrix(matrix, target));
}
static boolean searchMatrix(int[][] matrix, int target) {
int r = 0;
int c = matrix[0].length-1;
while(r < matrix.length && c >= 0){
if(matrix[r][c] == target){
return true;
}
else if(matrix[r][c] > target){
c--;
}
else if(matrix[r][c] < target){
r++;
}
}
return false;
}
}