Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitede59f7

Browse files
committed
074 (3) update tests
1 parent0f4f668 commitede59f7

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
publicclassPractice {
3232

3333
publicbooleansearchMatrix(int[][]matrix,inttarget) {
34-
if (matrix.length ==0 ||matrix[0].length ==0) {
35-
returnfalse;
36-
}
37-
introws =matrix.length;
38-
intcols =matrix[0].length;
39-
intstartRow =0;
40-
intendRow =rows -1;
41-
while (startRow <endRow) {
42-
inthalfRow = (startRow +endRow) /2;
43-
inthalf =matrix[halfRow][cols -1];
44-
if (half ==target) {
45-
returntrue;
46-
}elseif (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-
returnsearchLine(matrix[startRow],target);
56-
}
57-
58-
privatebooleansearchLine(int[]line,inttarget) {
59-
if (line.length ==0) {
60-
returnfalse;
61-
}
62-
intleft =0;
63-
intright =line.length -1;
64-
while (left <=right) {
65-
intmid = (left +right) >>1;
66-
if (line[mid] ==target) {
67-
returntrue;
68-
}elseif (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
returnfalse;
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
publicclassPracticeTest {
1212

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

1616
@Rule
1717
publicTimeoutglobalTimeout =newTimeout(200);
1818

1919
@Before
2020
publicvoidsetUp()throwsException {
21-
solution =newSolution();
21+
solution =newPractice();
2222
}
2323

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

2929
@Test
3030
publicvoidTest1() {
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
inttarget =3;
3437
booleanactual =solution.searchMatrix(matrix,target);
3538
booleanexpected =true;
@@ -38,8 +41,11 @@ public void Test1() {
3841

3942
@Test
4043
publicvoidTest2() {
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
inttarget =100;
4450
booleanactual =solution.searchMatrix(matrix,target);
4551
booleanexpected =false;
@@ -48,8 +54,11 @@ public void Test2() {
4854

4955
@Test
5056
publicvoidTest3() {
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
inttarget =15;
5463
booleanactual =solution.searchMatrix(matrix,target);
5564
booleanexpected =false;
@@ -58,8 +67,11 @@ public void Test3() {
5867

5968
@Test
6069
publicvoidTest4() {
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
inttarget =30;
6476
booleanactual =solution.searchMatrix(matrix,target);
6577
booleanexpected =true;
@@ -68,8 +80,11 @@ public void Test4() {
6880

6981
@Test
7082
publicvoidTest5() {
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
inttarget =0;
7489
booleanactual =solution.searchMatrix(matrix,target);
7590
booleanexpected =false;
@@ -78,12 +93,127 @@ public void Test5() {
7893

7994
@Test
8095
publicvoidTest6() {
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
inttarget =10;
84102
booleanactual =solution.searchMatrix(matrix,target);
85103
booleanexpected =true;
86104
assertEquals(expected,actual);
87105
}
88106

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp