forked from Google-Developer-Student-Club-MBU/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixSearch.java
More file actions
44 lines (37 loc) · 1.29 KB
/
MatrixSearch.java
File metadata and controls
44 lines (37 loc) · 1.29 KB
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
public class MatrixSearch {
public static boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int rows = matrix.length;
int cols = matrix[0].length;
// Start from the top-right corner of the matrix
int row = 0;
int col = cols - 1;
while (row < rows && col >= 0) {
if (matrix[row][col] == target) {
return true; // Target found
} else if (matrix[row][col] < target) {
row++; // Move down if the current value is less than the target
} else {
col--; // Move left if the current value is greater than the target
}
}
return false; // Target not found
}
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 = 5;
boolean found = searchMatrix(matrix, target);
if (found) {
System.out.println(target + " is in the matrix.");
} else {
System.out.println(target + " is not in the matrix.");
}
}
}