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

Commit488a1de

Browse files
committed
153 (3) add template solution and new tests
1 parent1c24038 commit488a1de

File tree

4 files changed

+140
-22
lines changed

4 files changed

+140
-22
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Time : O(lgN) ; Space: O(1)
3+
* @tag : Array; Binary Search
4+
* @by : Steven Cooks
5+
* @date: Aug 4, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
10+
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
11+
*
12+
* Find the minimum element. You may assume no duplicate exists in the array.
13+
*
14+
***************************************************************************
15+
* {@link https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ }
16+
*/
17+
package_153_FindMinimumInRotatedSortedArray;
18+
19+
/** see test {@link _153_FindMinimumInRotatedSortedArray.SolutionTemplateTest } */
20+
publicclassSolutionTemplate {
21+
22+
// solution that using binary search template
23+
publicintfindMin(int[]nums) {
24+
if (nums.length ==0) {
25+
return0;
26+
}
27+
intleft =0;
28+
intright =nums.length -1;
29+
while (left +1 <right) {
30+
intmid =left + (right -left) /2;
31+
if (nums[mid] >nums[right]) {
32+
left =mid;
33+
}else {
34+
right =mid;
35+
}
36+
}
37+
returnMath.min(nums[left],nums[right]);
38+
}
39+
40+
}

‎test/_153_FindMinimumInRotatedSortedArray/PracticeTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,28 @@ public void tearDown() throws Exception {
2828

2929
@Test
3030
publicvoidTest1() {
31-
int[]nums = {1 };
31+
int[]nums = {2 };
3232
intactual =solution.findMin(nums);
33-
intexpected =1;
33+
intexpected =2;
3434
assertEquals(expected,actual);
3535
}
3636

3737
@Test
3838
publicvoidTest2() {
39-
int[]nums = {1,2,3 };
39+
int[]nums = {0,1,2,3 };
4040
intactual =solution.findMin(nums);
41-
intexpected =1;
41+
intexpected =0;
4242
assertEquals(expected,actual);
4343
}
4444

45-
4645
@Test
4746
publicvoidTest3() {
48-
int[]nums = {2,3,1 };
47+
int[]nums = {3,4,2 };
4948
intactual =solution.findMin(nums);
50-
intexpected =1;
49+
intexpected =2;
5150
assertEquals(expected,actual);
5251
}
5352

54-
5553
@Test
5654
publicvoidTest4() {
5755
int[]nums = {3,1,2 };
@@ -60,16 +58,14 @@ public void Test4() {
6058
assertEquals(expected,actual);
6159
}
6260

63-
6461
@Test
6562
publicvoidTest5() {
66-
int[]nums = {0,1,2,3,4 };
63+
int[]nums = {0,1,2,3,4,5 };
6764
intactual =solution.findMin(nums);
6865
intexpected =0;
6966
assertEquals(expected,actual);
7067
}
7168

72-
7369
@Test
7470
publicvoidTest6() {
7571
int[]nums = {4,0,1,2,3 };
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package_153_FindMinimumInRotatedSortedArray;
2+
3+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
publicclassSolutionTemplateTest {
12+
13+
/** Test method for {@link _153_FindMinimumInRotatedSortedArray.SolutionTemplate } */
14+
SolutionTemplatesolution;
15+
16+
@Rule
17+
publicTimeoutglobalTimeout =newTimeout(200);
18+
19+
@Before
20+
publicvoidsetUp()throwsException {
21+
solution =newSolutionTemplate();
22+
}
23+
24+
@After
25+
publicvoidtearDown()throwsException {
26+
solution =null;
27+
}
28+
29+
@Test
30+
publicvoidTest1() {
31+
int[]nums = {2 };
32+
intactual =solution.findMin(nums);
33+
intexpected =2;
34+
assertEquals(expected,actual);
35+
}
36+
37+
@Test
38+
publicvoidTest2() {
39+
int[]nums = {0,1,2,3 };
40+
intactual =solution.findMin(nums);
41+
intexpected =0;
42+
assertEquals(expected,actual);
43+
}
44+
45+
@Test
46+
publicvoidTest3() {
47+
int[]nums = {3,4,2 };
48+
intactual =solution.findMin(nums);
49+
intexpected =2;
50+
assertEquals(expected,actual);
51+
}
52+
53+
@Test
54+
publicvoidTest4() {
55+
int[]nums = {3,1,2 };
56+
intactual =solution.findMin(nums);
57+
intexpected =1;
58+
assertEquals(expected,actual);
59+
}
60+
61+
@Test
62+
publicvoidTest5() {
63+
int[]nums = {0,1,2,3,4,5 };
64+
intactual =solution.findMin(nums);
65+
intexpected =0;
66+
assertEquals(expected,actual);
67+
}
68+
69+
@Test
70+
publicvoidTest6() {
71+
int[]nums = {4,0,1,2,3 };
72+
intactual =solution.findMin(nums);
73+
intexpected =0;
74+
assertEquals(expected,actual);
75+
}
76+
77+
78+
@Test
79+
publicvoidTest7() {
80+
int[]nums = {2,3,4,0,1 };
81+
intactual =solution.findMin(nums);
82+
intexpected =0;
83+
assertEquals(expected,actual);
84+
}
85+
86+
}

‎test/_153_FindMinimumInRotatedSortedArray/SolutionTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,28 @@ public void tearDown() throws Exception {
2828

2929
@Test
3030
publicvoidTest1() {
31-
int[]nums = {1 };
31+
int[]nums = {2 };
3232
intactual =solution.findMin(nums);
33-
intexpected =1;
33+
intexpected =2;
3434
assertEquals(expected,actual);
3535
}
3636

3737
@Test
3838
publicvoidTest2() {
39-
int[]nums = {1,2,3 };
39+
int[]nums = {0,1,2,3 };
4040
intactual =solution.findMin(nums);
41-
intexpected =1;
41+
intexpected =0;
4242
assertEquals(expected,actual);
4343
}
4444

45-
4645
@Test
4746
publicvoidTest3() {
48-
int[]nums = {2,3,1 };
47+
int[]nums = {3,4,2 };
4948
intactual =solution.findMin(nums);
50-
intexpected =1;
49+
intexpected =2;
5150
assertEquals(expected,actual);
5251
}
5352

54-
5553
@Test
5654
publicvoidTest4() {
5755
int[]nums = {3,1,2 };
@@ -60,16 +58,14 @@ public void Test4() {
6058
assertEquals(expected,actual);
6159
}
6260

63-
6461
@Test
6562
publicvoidTest5() {
66-
int[]nums = {0,1,2,3,4 };
63+
int[]nums = {0,1,2,3,4,5 };
6764
intactual =solution.findMin(nums);
6865
intexpected =0;
6966
assertEquals(expected,actual);
7067
}
7168

72-
7369
@Test
7470
publicvoidTest6() {
7571
int[]nums = {4,0,1,2,3 };

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp