Skip to content

Commit 7122a3d

Browse files
committed
Updated search 2d matrix
1 parent 7760432 commit 7122a3d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Medium/Search2DMatrix.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,25 @@
2121
*/
2222
class Search2DMatrix {
2323
public static void main(String[] args) {
24+
Search2DMatrix s = new Search2DMatrix();
2425
int[][] matrix = { {1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50}};
25-
System.out.println(searchMatrix(matrix, 0));
26-
System.out.println(searchMatrix(matrix, 1));
27-
System.out.println(searchMatrix(matrix, 2));
28-
System.out.println(searchMatrix(matrix, 11));
29-
System.out.println(searchMatrix(matrix, 15));
30-
System.out.println(searchMatrix(matrix, 34));
31-
System.out.println(searchMatrix(matrix, 35));
32-
System.out.println(searchMatrix(matrix, 50));
33-
System.out.println(searchMatrix(matrix, 51));
34-
System.out.println(searchMatrix(matrix, 100));
26+
System.out.println(s.searchMatrix(matrix, 0));
27+
System.out.println(s.searchMatrix(matrix, 1));
28+
System.out.println(s.searchMatrix(matrix, 2));
29+
System.out.println(s.searchMatrix(matrix, 11));
30+
System.out.println(s.searchMatrix(matrix, 15));
31+
System.out.println(s.searchMatrix(matrix, 34));
32+
System.out.println(s.searchMatrix(matrix, 35));
33+
System.out.println(s.searchMatrix(matrix, 50));
34+
System.out.println(s.searchMatrix(matrix, 51));
35+
System.out.println(s.searchMatrix(matrix, 100));
3536
}
3637

3738
/**
3839
* Binary search to locate row, then binary search in a row
3940
* O(logm + logn)
4041
*/
41-
public static boolean searchMatrix(int[][] matrix, int target) {
42+
public boolean searchMatrix(int[][] matrix, int target) {
4243
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
4344
int left = 0;
4445
int right = matrix.length - 1;
@@ -69,7 +70,7 @@ public static boolean searchMatrix(int[][] matrix, int target) {
6970
*
7071
* disadvantage: 1. m * n may overflow 2. / and % are expensive
7172
*/
72-
public static boolean searchMatrixBest(int[][] matrix, int target) {
73+
public boolean searchMatrixBest(int[][] matrix, int target) {
7374
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
7475
int m = matrix.length;
7576
int n = matrix[0].length;
@@ -85,4 +86,4 @@ public static boolean searchMatrixBest(int[][] matrix, int target) {
8586
return false;
8687
}
8788

88-
}
89+
}

0 commit comments

Comments
 (0)