Skip to content

Commit ede59f7

Browse files
committed
074 (3) update tests
1 parent 0f4f668 commit ede59f7

File tree

4 files changed

+291
-71
lines changed

4 files changed

+291
-71
lines changed

src/_074_SearchA2DMatrix/Practice.java

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,12 @@
2727
*/
2828
package _074_SearchA2DMatrix;
2929

30-
/** see test {@link _074_SearchA2DMatrix.SolutionTest } */
30+
/** see test {@link _074_SearchA2DMatrix.PracticeTest } */
3131
public class Practice {
3232

3333
public boolean searchMatrix(int[][] matrix, int target) {
34-
if (matrix.length == 0 || matrix[0].length == 0) {
35-
return false;
36-
}
37-
int rows = matrix.length;
38-
int cols = matrix[0].length;
39-
int startRow = 0;
40-
int endRow = rows - 1;
41-
while (startRow < endRow) {
42-
int halfRow = (startRow + endRow) / 2;
43-
int half = matrix[halfRow][cols - 1];
44-
if (half == target) {
45-
return true;
46-
} else if (half < target) {
47-
// go to lower rows to find target
48-
startRow = halfRow + 1;
49-
} else {
50-
// !target can be in end row
51-
endRow = halfRow;
52-
}
53-
}
54-
// search in 1D array
55-
return searchLine(matrix[startRow], target);
56-
}
57-
58-
private boolean searchLine(int[] line, int target) {
59-
if (line.length == 0) {
60-
return false;
61-
}
62-
int left = 0;
63-
int right = line.length - 1;
64-
while (left <= right) {
65-
int mid = (left + right) >> 1;
66-
if (line[mid] == target) {
67-
return true;
68-
} else if (line[mid] < target) {
69-
// go to right part
70-
left = mid + 1;
71-
} else {
72-
right = mid - 1;
73-
}
74-
}
34+
// TODO Auto-generated method stub
7535
return false;
7636
}
7737

78-
}
38+
}

src/_074_SearchA2DMatrix/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Time : O(); Space: O()
2+
* Time : O(lg(mn)); Space: O(1)
33
* @tag : Array; Binary Search
44
* @by : Steven Cooks
55
* @date: Jun 6, 2015

test/_074_SearchA2DMatrix/PracticeTest.java

Lines changed: 145 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
public class PracticeTest {
1212

13-
/** Test method for {@link _074_SearchA2DMatrix.Solution } */
14-
Solution solution;
13+
/** Test method for {@link _074_SearchA2DMatrix.Practice } */
14+
Practice solution;
1515

1616
@Rule
1717
public Timeout globalTimeout = new Timeout(200);
1818

1919
@Before
2020
public void setUp() throws Exception {
21-
solution = new Solution();
21+
solution = new Practice();
2222
}
2323

2424
@After
@@ -28,8 +28,11 @@ public void tearDown() throws Exception {
2828

2929
@Test
3030
public void Test1() {
31-
int[][] matrix = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 },
32-
{ 23, 30, 34, 50 } };
31+
int[][] matrix = {
32+
{ 1, 3, 5, 7 },
33+
{ 10, 11, 16, 20 },
34+
{ 23, 30, 34, 50 }
35+
};
3336
int target = 3;
3437
boolean actual = solution.searchMatrix(matrix, target);
3538
boolean expected = true;
@@ -38,8 +41,11 @@ public void Test1() {
3841

3942
@Test
4043
public void Test2() {
41-
int[][] matrix = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 },
42-
{ 23, 30, 34, 50 } };
44+
int[][] matrix = {
45+
{ 1, 3, 5, 7 },
46+
{ 10, 11, 16, 20 },
47+
{ 23, 30, 34, 50 }
48+
};
4349
int target = 100;
4450
boolean actual = solution.searchMatrix(matrix, target);
4551
boolean expected = false;
@@ -48,8 +54,11 @@ public void Test2() {
4854

4955
@Test
5056
public void Test3() {
51-
int[][] matrix = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 },
52-
{ 23, 30, 34, 50 } };
57+
int[][] matrix = {
58+
{ 1, 3, 5, 7 },
59+
{ 10, 11, 16, 20 },
60+
{ 23, 30, 34, 50 }
61+
};
5362
int target = 15;
5463
boolean actual = solution.searchMatrix(matrix, target);
5564
boolean expected = false;
@@ -58,8 +67,11 @@ public void Test3() {
5867

5968
@Test
6069
public void Test4() {
61-
int[][] matrix = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 },
62-
{ 23, 30, 34, 50 } };
70+
int[][] matrix = {
71+
{ 1, 3, 5, 7 },
72+
{ 10, 11, 16, 20 },
73+
{ 23, 30, 34, 50 }
74+
};
6375
int target = 30;
6476
boolean actual = solution.searchMatrix(matrix, target);
6577
boolean expected = true;
@@ -68,8 +80,11 @@ public void Test4() {
6880

6981
@Test
7082
public void Test5() {
71-
int[][] matrix = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 },
72-
{ 23, 30, 34, 50 } };
83+
int[][] matrix = {
84+
{ 1, 3, 5, 7 },
85+
{ 10, 11, 16, 20 },
86+
{ 23, 30, 34, 50 }
87+
};
7388
int target = 0;
7489
boolean actual = solution.searchMatrix(matrix, target);
7590
boolean expected = false;
@@ -78,12 +93,127 @@ public void Test5() {
7893

7994
@Test
8095
public void Test6() {
81-
int[][] matrix = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 },
82-
{ 23, 30, 34, 50 } };
96+
int[][] matrix = {
97+
{ 1, 3, 5, 7 },
98+
{ 10, 11, 16, 20 },
99+
{ 23, 30, 34, 50 }
100+
};
83101
int target = 10;
84102
boolean actual = solution.searchMatrix(matrix, target);
85103
boolean expected = true;
86104
assertEquals(expected, actual);
87105
}
88106

107+
@Test
108+
public void Test7() {
109+
int[][] matrix = { { 1, 3, 5, 7 } };
110+
int target = 10;
111+
assertFalse(solution.searchMatrix(matrix, target));
112+
}
113+
114+
@Test
115+
public void Test8() {
116+
int[][] matrix = { { 1, 3, 5, 7 } };
117+
int target = 7;
118+
assertTrue(solution.searchMatrix(matrix, target));
119+
}
120+
121+
@Test
122+
public void Test9() {
123+
int[][] matrix = { { 1, 3, 5, 7 } };
124+
int target = 1;
125+
assertTrue(solution.searchMatrix(matrix, target));
126+
}
127+
128+
@Test
129+
public void Test10() {
130+
int[][] matrix = { { 1, 3, 5, 7 } };
131+
int target = 4;
132+
assertFalse(solution.searchMatrix(matrix, target));
133+
}
134+
135+
@Test
136+
public void Test11() {
137+
int[][] matrix = { { 1, 3, 5, 7 } };
138+
int target = 5;
139+
assertTrue(solution.searchMatrix(matrix, target));
140+
}
141+
142+
@Test
143+
public void Test12() {
144+
int[][] matrix = {
145+
{ 1 },
146+
{ 5 },
147+
{ 9 },
148+
};
149+
int target = 9;
150+
assertTrue(solution.searchMatrix(matrix, target));
151+
}
152+
153+
@Test
154+
public void Test13() {
155+
int[][] matrix = {
156+
{ 1 },
157+
{ 5 },
158+
{ 9 },
159+
};
160+
int target = 1;
161+
assertTrue(solution.searchMatrix(matrix, target));
162+
}
163+
164+
@Test
165+
public void Test14() {
166+
int[][] matrix = {
167+
{ 1 },
168+
{ 5 },
169+
{ 9 },
170+
};
171+
int target = 5;
172+
assertTrue(solution.searchMatrix(matrix, target));
173+
}
174+
175+
@Test
176+
public void Test15() {
177+
int[][] matrix = {
178+
{ 1 },
179+
{ 5 },
180+
{ 9 },
181+
};
182+
int target = 18;
183+
assertFalse(solution.searchMatrix(matrix, target));
184+
}
185+
186+
@Test
187+
public void Test16() {
188+
int[][] matrix = {
189+
{ 1 },
190+
{ 5 },
191+
{ 9 },
192+
};
193+
int target = 0;
194+
assertFalse(solution.searchMatrix(matrix, target));
195+
}
196+
197+
@Test
198+
public void Test17() {
199+
int[][] matrix = {
200+
{ 1 },
201+
{ 5 },
202+
{ 9 },
203+
};
204+
int target = 3;
205+
assertFalse(solution.searchMatrix(matrix, target));
206+
}
207+
208+
@Test
209+
public void Test18() {
210+
int[][] matrix = {
211+
{ 1 },
212+
{ 5 },
213+
{ 9 },
214+
};
215+
int target = 7;
216+
assertFalse(solution.searchMatrix(matrix, target));
217+
}
218+
89219
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy