File tree Expand file tree Collapse file tree 3 files changed +77
-0
lines changed
main/java/com/stevesun/solutions Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 299
299
|78|[ Subsets] ( https://leetcode.com/problems/subsets/ ) |[ Solution] ( ../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Subsets.java ) |O(n^2) ? |O(1)|Medium|Backtracking
300
300
|76|[ Minimum Window Substring] ( https://leetcode.com/problems/minimum-window-substring/ ) |[ Solution] ( ../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumWindowSubstring.java ) |O(n)|O(k)|Hard|Two Pointers
301
301
|75|[ Sort Colors] ( https://leetcode.com/problems/sort-colors/ ) |[ Solution] ( ../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SortColors.java ) |O(n)|O(1)|Medium| Two Pointers
302
+ |74|[ Search a 2D Matrix] ( https://leetcode.com/problems/search-a-2d-matrix/ ) |[ Solution] ( ../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Searcha2DMatrix.java ) |O(logn)|O(1)|Medium| Binary Search
302
303
| 73| [ Set Matrix Zeroes] ( https://leetcode.com/problems/set-matrix-zeroes/ ) | [ Solution] ( ../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SetMatrixZeroes.java ) | O(mn)| O(mn)| Medium|
303
304
| 72| [ Edit Distance] ( https://leetcode.com/problems/edit-distance/ ) | [ Solution] ( ../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EditDistance.java ) | O(m* n)| O(m+n)| Hard|
304
305
|71|[ Simplify Path] ( https://leetcode.com/problems/simplify-path/ ) |[ Solution] ( ../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SimplifyPath.java ) |O(n)|O(n)|Medium| Stack
Original file line number Diff line number Diff line change
1
+ package com .stevesun .solutions ;
2
+
3
+ /**
4
+ * Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
5
+
6
+ Integers in each row are sorted from left to right.
7
+ The first integer of each row is greater than the last integer of the previous row.
8
+ For example,
9
+
10
+ Consider the following matrix:
11
+
12
+ [
13
+ [1, 3, 5, 7],
14
+ [10, 11, 16, 20],
15
+ [23, 30, 34, 50]
16
+ ]
17
+
18
+ Given target = 3, return true.
19
+
20
+ */
21
+ public class Searcha2DMatrix {
22
+
23
+ public boolean searchMatrix (int [][] matrix , int target ) {
24
+ if (matrix == null || matrix .length == 0 || matrix [0 ].length == 0 ) return false ;
25
+ int m = matrix .length , n = matrix [0 ].length ;
26
+ if (target < matrix [0 ][0 ] || target > matrix [m -1 ][n -1 ]) return false ;
27
+ //just treat it as a sorted list
28
+ int left = 0 ;
29
+ int right = m *n - 1 ;
30
+ while (left != right ) {
31
+ int mid = (left + right - 1 ) >> 1 ;
32
+ if (matrix [mid /n ][mid %n ] < target ) left = mid + 1 ;
33
+ else right = mid ;
34
+ }
35
+ return matrix [right /n ][right %n ] == target ;
36
+ }
37
+
38
+ }
Original file line number Diff line number Diff line change
1
+ package com .stevesun ;
2
+
3
+ import com .stevesun .solutions .Searcha2DMatrix ;
4
+ import org .junit .Before ;
5
+ import org .junit .BeforeClass ;
6
+ import org .junit .Test ;
7
+
8
+ import static junit .framework .Assert .assertEquals ;
9
+
10
+ public class Searcha2DMatrixTest {
11
+ private static Searcha2DMatrix test ;
12
+ private static boolean actual ;
13
+ private static boolean expected ;
14
+ private static int target ;
15
+ private static int [][] matrix ;
16
+
17
+ @ BeforeClass
18
+ public static void setup (){
19
+ test = new Searcha2DMatrix ();
20
+ }
21
+
22
+ @ Before
23
+ public void setupForEachTest (){
24
+ }
25
+
26
+ @ Test
27
+ public void test1 (){
28
+ target = 3 ;
29
+ matrix = new int [][]{
30
+ {1 , 3 , 5 , 7 },
31
+ {10 , 11 , 16 , 20 },
32
+ {23 , 30 , 34 , 50 },
33
+ };
34
+ expected = true ;
35
+ actual = test .searchMatrix (matrix , target );
36
+ assertEquals (expected , actual );
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments