-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion46.java
More file actions
32 lines (26 loc) · 790 Bytes
/
question46.java
File metadata and controls
32 lines (26 loc) · 790 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
public class question46 {
public static boolean searchMatrix(int[][] matrix, int target) {
int row = 0;
int col = matrix[0].length - 1;
while (row < matrix.length && col >= 0) {
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] > target) {
col--;
} else {
row++;
}
}
return false;
}
public static void main(String[] args) {
int[][] matrix = {
{1, 4, 7, 11},
{2, 5, 8, 12},
{3, 6, 9, 16},
{10, 13, 14, 17}
};
int target = 14;
System.out.println("Found: " + searchMatrix(matrix, target));
}
}