Skip to content

Commit 0b14e5e

Browse files
committed
search 2D matrix
1 parent a673b57 commit 0b14e5e

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package joshua.leetcode.array.binarysearch;
2+
3+
import joshua.leetcode.solutiontag.*;
4+
import joshua.leetcode.solutiontag.BinarySearch;
5+
6+
/**
7+
* 74. Search a 2D Matrix<br/>
8+
* <p/>
9+
* <a href ="https://leetcode.com/problems/search-a-2d-matrix/">leetcode link</a>
10+
*
11+
* @author Joshua.Jiang on 2016/5/29.
12+
*/
13+
public abstract class Search2DMatrix {
14+
15+
/**
16+
* Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
17+
* <p/>
18+
* Integers in each row are sorted from left to right.
19+
* The first integer of each row is greater than the last integer of the previous row.
20+
* <p/>
21+
* <p/>
22+
* For example,
23+
* Consider the following matrix:
24+
* <p/>
25+
* [
26+
* [1, 3, 5, 7],
27+
* [10, 11, 16, 20],
28+
* [23, 30, 34, 50]
29+
* ]
30+
* <p/>
31+
* Given target = 3, return true
32+
*
33+
* @param matrix
34+
* @param target
35+
* @return
36+
*/
37+
public abstract boolean searchMatrix(int[][] matrix, int target);
38+
39+
40+
/**
41+
* 先根据最后一列确定在哪一行,再对该行做binary search
42+
*/
43+
@BinarySearch
44+
public static class Solution1 extends Search2DMatrix {
45+
46+
@Override
47+
public boolean searchMatrix(int[][] matrix, int target) {
48+
if (matrix == null || matrix[0] == null || matrix[0].length == 0) {
49+
return false;
50+
}
51+
int row = matrix.length;
52+
int col = matrix[0].length;
53+
// first binary search
54+
int beg = 0, end = row - 1;
55+
while (beg <= end) {
56+
int mid = (beg + end) / 2;
57+
if (matrix[mid][col - 1] > target) {
58+
end = mid - 1;
59+
} else if (matrix[mid][col - 1] < target) {
60+
beg = mid + 1;
61+
} else {
62+
return true;
63+
}
64+
}
65+
//boundary case, for example if target is 70 in the above example.
66+
if (beg == row) {
67+
return false;
68+
}
69+
//now need to search row indexed by 'beg'
70+
int rowBeg = 0, rowEnd = col - 1;
71+
while (rowBeg <= rowEnd) {
72+
int rowMid = (rowBeg + rowEnd) / 2;
73+
if(matrix[beg][rowMid] == target) {
74+
return true;
75+
}
76+
if (matrix[beg][rowMid] < target) {
77+
rowBeg = rowMid + 1;
78+
} else {
79+
rowEnd = rowMid - 1;
80+
}
81+
}
82+
return false;
83+
}
84+
}
85+
}

src/main/java/joshua/leetcode/divideconquer/KthLargestElementInArray.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package joshua.leetcode.divideconquer;
22

3+
import joshua.leetcode.solutiontag.MinHeap;
4+
35
/**
46
* Kth Largest Element in an Array
57
*
@@ -25,6 +27,7 @@ public abstract class KthLargestElementInArray {
2527

2628
/**
2729
* Divide and Conquer way.
30+
*
2831
* partition the array using quick-sorting way(descending sort), every pass find the final position of an element.
2932
* If this position's index is bigger than k-1, quick sort the partition on left, otherwise right.
3033
* function returns if position is found at index k-1.
@@ -71,6 +74,7 @@ private int partition(int[] nums, int start, int end) {
7174
*
7275
* @author joy
7376
*/
77+
@MinHeap
7478
static class Solution2 extends KthLargestElementInArray {
7579

7680
@Override
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package joshua.leetcode.divideconquer;
2+
3+
/**
4+
* 240. Search a 2D Matrix II<br/>
5+
* <a href = "https://leetcode.com/problems/search-a-2d-matrix-ii/">leetcode link</a>
6+
*
7+
* @author Joshua.Jiang on 2016/5/29.
8+
*/
9+
public abstract class Search2DMatrixII {
10+
11+
/**
12+
* Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
13+
* <ul>
14+
* <li>Integers in each row are sorted in ascending from left to right.</li>
15+
* <li>Integers in each column are sorted in ascending from top to bottom.</li>
16+
* For example,
17+
* </ul>
18+
* Consider the following matrix:
19+
* <p/>
20+
* [<br/>
21+
* [1, 4, 7, 11, 15],<br/>
22+
* [2, 5, 8, 12, 19],<br/>
23+
* [3, 6, 9, 16, 22],<br/>
24+
* [10, 13, 14, 17, 24],<br/>
25+
* [18, 21, 23, 26, 30]<br/>
26+
* ]<br/>
27+
* Given target = 5, return true.
28+
* Given target = 20, return false.
29+
*
30+
* @param matrix
31+
* @param target
32+
* @return
33+
*/
34+
public abstract boolean searchMatrix(int[][] matrix, int target);
35+
36+
/**
37+
* 观察矩阵可以发现, 每个对角线上的元素matrix[i,i]都大于所有其他index小于i的元素,即该元素的左上角的矩阵。
38+
* 因此可以先对对角线上元素做二分查找。
39+
*
40+
*/
41+
public static class Solution1 extends Search2DMatrixII {
42+
43+
@Override
44+
public boolean searchMatrix(int[][] matrix, int target) {
45+
46+
}
47+
}
48+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package joshua.leetcode.solutiontag;
2+
3+
/**
4+
* @author Joshua.Jiang on 2016/5/29.
5+
*/
6+
public @interface BinarySearch {
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package joshua.leetcode.array.binarysearch;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class Search2DMatrixTest {
9+
10+
private Search2DMatrix solution;
11+
12+
@Before
13+
public void setUp() {
14+
solution = new Search2DMatrix.Solution1();
15+
}
16+
17+
@Test
18+
public void testSolution() {
19+
int[][] matrix = new int[][]{
20+
{1, 3, 5, 7},
21+
{10, 11, 16, 20},
22+
{23, 30, 34, 50}
23+
};
24+
assertTrue(solution.searchMatrix(matrix, 7));
25+
assertTrue(solution.searchMatrix(matrix, 3));
26+
assertTrue(solution.searchMatrix(matrix, 30));
27+
assertTrue(solution.searchMatrix(matrix, 50));
28+
29+
assertFalse(solution.searchMatrix(matrix, -1));
30+
assertFalse(solution.searchMatrix(matrix, 8));
31+
assertFalse(solution.searchMatrix(matrix, 70));
32+
}
33+
34+
35+
}

0 commit comments

Comments
 (0)