|
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * Two variations of searching in a 2D matrix |
5 | | - * |
| 5 | + * |
6 | 6 | * Variation 1: |
7 | 7 | * - Each row is sorted left to right |
8 | 8 | * - Each column is sorted top to bottom |
9 | | - * |
| 9 | + * |
10 | 10 | * Variation 2: |
11 | 11 | * - The matrix is a flattened sorted array |
12 | 12 | * - i.e., row 0 ends, row 1 continues (matrix is sorted as a 1D array) |
13 | | - * |
| 13 | + * |
14 | 14 | * LeetCode: #74 and #240 |
15 | 15 | */ |
16 | 16 | public class Search2DMatrix { |
17 | 17 |
|
18 | 18 | /** |
19 | | - * Variation 1: Search in a matrix where each row and each column is sorted |
20 | | - * Time: O(m + n) |
| 19 | + * Search in a 2D matrix where rows and columns are sorted. |
21 | 20 | * |
22 | | - * @param matrix input matrix |
23 | | - * @param target value to find |
24 | | - * @return true if found, false otherwise |
| 21 | + * @param matrix The 2D matrix with sorted rows and columns. |
| 22 | + * @param target The target integer to search for. |
| 23 | + * @return true if target is found, otherwise false. |
25 | 24 | */ |
26 | 25 | public static boolean searchMatrixSortedRowsAndCols(int[][] matrix, int target) { |
27 | | - int rows = matrix.length; |
28 | | - if (rows == 0) return false; |
| 26 | + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { |
| 27 | + return false; |
| 28 | + } |
29 | 29 |
|
| 30 | + int rows = matrix.length; |
30 | 31 | int cols = matrix[0].length; |
31 | | - int row = 0, col = cols - 1; |
| 32 | + |
| 33 | + int row = 0; |
| 34 | + int col = cols - 1; |
32 | 35 |
|
33 | 36 | while (row < rows && col >= 0) { |
34 | 37 | int val = matrix[row][col]; |
35 | | - if (val == target) return true; |
36 | | - else if (val > target) col--; |
37 | | - else row++; |
| 38 | + if (val == target) { |
| 39 | + return true; |
| 40 | + } else if (val > target) { |
| 41 | + col--; |
| 42 | + } else { |
| 43 | + row++; |
| 44 | + } |
38 | 45 | } |
39 | 46 |
|
40 | 47 | return false; |
41 | 48 | } |
42 | 49 |
|
43 | 50 | /** |
44 | | - * Variation 2: Search in a matrix that behaves like a flattened sorted array |
45 | | - * Time: O(log(m * n)) |
| 51 | + * Search in a 2D matrix that is sorted as if flattened into a 1D array. |
46 | 52 | * |
47 | | - * @param matrix input matrix |
48 | | - * @param target value to find |
49 | | - * @return true if found, false otherwise |
| 53 | + * @param matrix The 2D matrix. |
| 54 | + * @param target The target integer to search for. |
| 55 | + * @return true if target is found, otherwise false. |
50 | 56 | */ |
51 | 57 | public static boolean searchMatrixFlattenedSorted(int[][] matrix, int target) { |
52 | | - int rows = matrix.length; |
53 | | - if (rows == 0) return false; |
| 58 | + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { |
| 59 | + return false; |
| 60 | + } |
54 | 61 |
|
| 62 | + int rows = matrix.length; |
55 | 63 | int cols = matrix[0].length; |
56 | | - int left = 0, right = rows * cols - 1; |
| 64 | + |
| 65 | + int left = 0; |
| 66 | + int right = rows * cols - 1; |
57 | 67 |
|
58 | 68 | while (left <= right) { |
59 | 69 | int mid = left + (right - left) / 2; |
60 | 70 | int val = matrix[mid / cols][mid % cols]; |
61 | 71 |
|
62 | | - if (val == target) return true; |
63 | | - else if (val < target) left = mid + 1; |
64 | | - else right = mid - 1; |
| 72 | + if (val == target) { |
| 73 | + return true; |
| 74 | + } else if (val < target) { |
| 75 | + left = mid + 1; |
| 76 | + } else { |
| 77 | + right = mid - 1; |
| 78 | + } |
65 | 79 | } |
66 | 80 |
|
67 | 81 | return false; |
68 | 82 | } |
69 | | - |
70 | | - // Example usage |
71 | | - public static void main(String[] args) { |
72 | | - int[][] matrix1 = { |
73 | | - {1, 4, 7, 11}, |
74 | | - {2, 5, 8, 12}, |
75 | | - {3, 6, 9, 16}, |
76 | | - {10, 13, 14, 17} |
77 | | - }; |
78 | | - System.out.println(searchMatrixSortedRowsAndCols(matrix1, 5)); // true |
79 | | - |
80 | | - int[][] matrix2 = { |
81 | | - {1, 3, 5, 7}, |
82 | | - {10, 11, 16, 20}, |
83 | | - {23, 30, 34, 50} |
84 | | - }; |
85 | | - System.out.println(searchMatrixFlattenedSorted(matrix2, 3)); // true |
86 | | - } |
87 | 83 | } |
0 commit comments