Skip to content

Commit 1bf43e4

Browse files
refactor 153
1 parent d2815b5 commit 1bf43e4

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
public class _153 {
14-
24+
public static class Solution1 {
1525
public int findMin(int[] nums) {
16-
int left = 0;
17-
int right = nums.length - 1;
18-
if (nums[left] < nums[right]) {
19-
return nums[left];
20-
}
21-
int min = nums[0];
22-
while (left + 1 < right) {
23-
int mid = 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-
} else if (nums[mid] < nums[left]) {
29-
right = mid - 1;
30-
}
26+
int left = 0;
27+
int right = nums.length - 1;
28+
if (nums[left] < nums[right]) {
29+
return nums[left];
30+
}
31+
int min = nums[0];
32+
while (left + 1 < right) {
33+
int mid = 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+
} else if (nums[mid] < nums[left]) {
39+
right = mid - 1;
3140
}
32-
min = Math.min(min, Math.min(nums[left], nums[right]));
33-
return min;
41+
}
42+
min = Math.min(min, Math.min(nums[left], nums[right]));
43+
return min;
3444
}
35-
45+
}
3646
}
Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,51 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._153;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static org.junit.Assert.assertEquals;
98

10-
/**
11-
* Created by fishercoder on 1/10/17.
12-
*/
139
public class _153Test {
14-
private static _153 test;
15-
private static int expected;
16-
private static int actual;
17-
private static int[] nums;
18-
19-
@BeforeClass
20-
public static void setup() {
21-
test = new _153();
22-
}
23-
24-
@Before
25-
public void setupForEachTest() {
26-
}
27-
28-
@Test
29-
public void test1() {
30-
31-
nums = new int[]{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-
public void test2() {
40-
nums = new int[]{1};
41-
expected = 1;
42-
actual = test.findMin(nums);
43-
assertEquals(expected, actual);
44-
}
45-
46-
@Test
47-
public void test3() {
48-
nums = new int[]{2, 1};
49-
expected = 1;
50-
actual = test.findMin(nums);
51-
assertEquals(expected, actual);
52-
}
53-
54-
@Test
55-
public void test4() {
56-
nums = new int[]{2, 3, 4, 5, 1};
57-
expected = 1;
58-
actual = test.findMin(nums);
59-
assertEquals(expected, actual);
60-
}
10+
private static _153.Solution1 solution1;
11+
private static int expected;
12+
private static int actual;
13+
private static int[] nums;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _153.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
nums = new int[] {4, 5, 6, 7, 0, 1, 2};
23+
expected = 0;
24+
actual = solution1.findMin(nums);
25+
assertEquals(expected, actual);
26+
}
27+
28+
@Test
29+
public void test2() {
30+
nums = new int[] {1};
31+
expected = 1;
32+
actual = solution1.findMin(nums);
33+
assertEquals(expected, actual);
34+
}
35+
36+
@Test
37+
public void test3() {
38+
nums = new int[] {2, 1};
39+
expected = 1;
40+
actual = solution1.findMin(nums);
41+
assertEquals(expected, actual);
42+
}
43+
44+
@Test
45+
public void test4() {
46+
nums = new int[] {2, 3, 4, 5, 1};
47+
expected = 1;
48+
actual = solution1.findMin(nums);
49+
assertEquals(expected, actual);
50+
}
6151
}

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