File tree Expand file tree Collapse file tree 3 files changed +57
-2
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 3 files changed +57
-2
lines changed Original file line number Diff line number Diff line change @@ -379,7 +379,7 @@ Your ideas/fixes/algorithms are more than welcome!
379
379
|243|[ Shortest Word Distance] ( https://leetcode.com/problems/shortest-word-distance/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_243.java ) | O(n) | O(1) | Easy
380
380
|242|[ Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_242.java ) | O(n) | O(1) | Easy
381
381
|241|[ Different Ways to Add Parentheses] ( https://leetcode.com/problems/different-ways-to-add-parentheses/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_241.java ) | O(O(n * 4^n / n^(3/2))) | O(n * 4^n / n^(3/2)) | Medium | Divide and Conquer
382
- |240|[ Search a 2D Matrix II] ( https://leetcode.com/problems/search-a-2d-matrix-ii/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_240.java ) | O(log(m * n) )|O(1) | Medium| Binary Search
382
+ |240|[ Search a 2D Matrix II] ( https://leetcode.com/problems/search-a-2d-matrix-ii/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_240.java ) | O(m+n )|O(1) | Medium| Binary Search
383
383
|239|[ Sliding Window Maximum] ( https://leetcode.com/problems/sliding-window-maximum/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_239.java ) | O(nlogn)|O(k) | Hard| Heap
384
384
|238|[ Product of Array Except Self] ( https://leetcode.com/problems/product-of-array-except-self/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_238.java ) | O(n)|O(1) | Medium| Array
385
385
|237|[ Delete Node in a Linked List] ( https://leetcode.com/problems/delete-node-in-a-linked-list/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_237.java ) | O(1)|O(1) | Easy| LinkedList
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
/**
4
- * Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
4
+ * 240. Search a 2D Matrix II
5
+ *
6
+ * Write an efficient algorithm that searches for a value in an m x n matrix.
7
+ * This matrix has the following properties:
5
8
6
9
Integers in each row are sorted in ascending from left to right.
7
10
Integers in each column are sorted in ascending from top to bottom.
23
26
public class _240 {
24
27
25
28
public boolean searchMatrix (int [][] matrix , int target ) {
29
+ if (matrix == null || matrix .length == 0 ) {
30
+ return false ;
31
+ }
26
32
int m = matrix .length ;
27
33
int n = matrix [0 ].length ;
28
34
int x = 0 ;
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._240 ;
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 _240Test {
11
+ private static _240 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 _240 ();
20
+ }
21
+
22
+ @ Before
23
+ public void setupForEachTest (){
24
+ }
25
+
26
+ @ Test
27
+ public void test1 (){
28
+ target = 5 ;
29
+ matrix = new int [][]{
30
+ {1 , 4 , 7 , 11 , 15 },
31
+ {2 , 5 , 8 , 12 , 19 },
32
+ {3 , 6 , 9 , 16 , 22 },
33
+ {10 , 13 , 14 , 17 , 24 },
34
+ {18 , 21 , 23 , 26 , 30 }
35
+ };
36
+ expected = true ;
37
+ actual = test .searchMatrix (matrix , target );
38
+ assertEquals (expected , actual );
39
+ }
40
+
41
+ @ Test
42
+ public void test2 (){
43
+ target = 0 ;
44
+ matrix = new int [][]{};
45
+ expected = false ;
46
+ actual = test .searchMatrix (matrix , target );
47
+ assertEquals (expected , actual );
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments