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

Commit1bf43e4

Browse files
refactor 153
1 parentd2815b5 commit1bf43e4

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

‎src/main/java/com/fishercoder/solutions/_153.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,45 @@
22

33
/**
44
* 153. Find Minimum in Rotated Sorted Array
5-
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
65
7-
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
6+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
7+
8+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
89
910
Find the minimum element.
1011
1112
You may assume no duplicate exists in the array.
13+
14+
Example 1:
15+
Input: [3,4,5,1,2]
16+
Output: 1
17+
18+
Example 2:
19+
Input: [4,5,6,7,0,1,2]
20+
Output: 0
21+
1222
*/
1323
publicclass_153 {
14-
24+
publicstaticclassSolution1 {
1525
publicintfindMin(int[]nums) {
16-
intleft =0;
17-
intright =nums.length -1;
18-
if (nums[left] <nums[right]) {
19-
returnnums[left];
20-
}
21-
intmin =nums[0];
22-
while (left +1 <right) {
23-
intmid =left + (right -left) /2;
24-
min =Math.min(min,nums[mid]);
25-
if (nums[mid] >nums[left]) {
26-
min =Math.min(nums[left],min);
27-
left =mid +1;
28-
}elseif (nums[mid] <nums[left]) {
29-
right =mid -1;
30-
}
26+
intleft =0;
27+
intright =nums.length -1;
28+
if (nums[left] <nums[right]) {
29+
returnnums[left];
30+
}
31+
intmin =nums[0];
32+
while (left +1 <right) {
33+
intmid =left + (right -left) /2;
34+
min =Math.min(min,nums[mid]);
35+
if (nums[mid] >nums[left]) {
36+
min =Math.min(nums[left],min);
37+
left =mid +1;
38+
}elseif (nums[mid] <nums[left]) {
39+
right =mid -1;
3140
}
32-
min =Math.min(min,Math.min(nums[left],nums[right]));
33-
returnmin;
41+
}
42+
min =Math.min(min,Math.min(nums[left],nums[right]));
43+
returnmin;
3444
}
35-
45+
}
3646
}
Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,51 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.solutions._153;
4-
importorg.junit.Before;
54
importorg.junit.BeforeClass;
65
importorg.junit.Test;
76

87
importstaticorg.junit.Assert.assertEquals;
98

10-
/**
11-
* Created by fishercoder on 1/10/17.
12-
*/
139
publicclass_153Test {
14-
privatestatic_153test;
15-
privatestaticintexpected;
16-
privatestaticintactual;
17-
privatestaticint[]nums;
18-
19-
@BeforeClass
20-
publicstaticvoidsetup() {
21-
test =new_153();
22-
}
23-
24-
@Before
25-
publicvoidsetupForEachTest() {
26-
}
27-
28-
@Test
29-
publicvoidtest1() {
30-
31-
nums =newint[]{4,5,6,7,0,1,2};
32-
expected =0;
33-
actual =test.findMin(nums);
34-
assertEquals(expected,actual);
35-
36-
}
37-
38-
@Test
39-
publicvoidtest2() {
40-
nums =newint[]{1};
41-
expected =1;
42-
actual =test.findMin(nums);
43-
assertEquals(expected,actual);
44-
}
45-
46-
@Test
47-
publicvoidtest3() {
48-
nums =newint[]{2,1};
49-
expected =1;
50-
actual =test.findMin(nums);
51-
assertEquals(expected,actual);
52-
}
53-
54-
@Test
55-
publicvoidtest4() {
56-
nums =newint[]{2,3,4,5,1};
57-
expected =1;
58-
actual =test.findMin(nums);
59-
assertEquals(expected,actual);
60-
}
10+
privatestatic_153.Solution1solution1;
11+
privatestaticintexpected;
12+
privatestaticintactual;
13+
privatestaticint[]nums;
14+
15+
@BeforeClass
16+
publicstaticvoidsetup() {
17+
solution1 =new_153.Solution1();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
22+
nums =newint[] {4,5,6,7,0,1,2};
23+
expected =0;
24+
actual =solution1.findMin(nums);
25+
assertEquals(expected,actual);
26+
}
27+
28+
@Test
29+
publicvoidtest2() {
30+
nums =newint[] {1};
31+
expected =1;
32+
actual =solution1.findMin(nums);
33+
assertEquals(expected,actual);
34+
}
35+
36+
@Test
37+
publicvoidtest3() {
38+
nums =newint[] {2,1};
39+
expected =1;
40+
actual =solution1.findMin(nums);
41+
assertEquals(expected,actual);
42+
}
43+
44+
@Test
45+
publicvoidtest4() {
46+
nums =newint[] {2,3,4,5,1};
47+
expected =1;
48+
actual =solution1.findMin(nums);
49+
assertEquals(expected,actual);
50+
}
6151
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp