Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0b1bd04

Browse files
refactor 240
1 parent efbd9a2 commit 0b1bd04

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ Your ideas/fixes/algorithms are more than welcome!
379379
|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
380380
|242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_242.java) | O(n) | O(1) | Easy
381381
|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
383383
|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
384384
|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
385385
|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

src/main/java/com/fishercoder/solutions/_240.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.fishercoder.solutions;
22

33
/**
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:
58
69
Integers in each row are sorted in ascending from left to right.
710
Integers in each column are sorted in ascending from top to bottom.
@@ -23,6 +26,9 @@
2326
public class _240 {
2427

2528
public boolean searchMatrix(int[][] matrix, int target) {
29+
if (matrix == null || matrix.length == 0) {
30+
return false;
31+
}
2632
int m = matrix.length;
2733
int n = matrix[0].length;
2834
int x = 0;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

0 commit comments

Comments
 (0)