Skip to content

Commit 5b8c58d

Browse files
refactor 154
1 parent beb6a18 commit 5b8c58d

File tree

1 file changed

+22
-46
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+22
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,28 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 154. Find Minimum in Rotated Sorted Array II
5-
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]).
9-
10-
Find the minimum element.
11-
12-
The array may contain duplicates.
13-
14-
Example 1:
15-
Input: [1,3,5]
16-
Output: 1
17-
18-
Example 2:
19-
Input: [2,2,2,0,1]
20-
Output: 0
21-
22-
Note:
23-
This is a follow up problem to Find Minimum in Rotated Sorted Array.
24-
Would allow duplicates affect the run-time complexity? How and why?
25-
26-
*/
273
public class _154 {
28-
public static class Solution1 {
29-
public int findMin(int[] nums) {
30-
int left = 0;
31-
int right = nums.length - 1;
32-
if (nums[left] < nums[right]) {
33-
return nums[left];
34-
}
35-
int min = nums[0];
36-
while (left + 1 < right) {
37-
int mid = left + (right - left) / 2;
38-
min = Math.min(min, nums[mid]);
39-
if (nums[mid] > nums[left]) {
40-
min = Math.min(nums[left], min);
41-
left = mid + 1;
42-
} else if (nums[mid] < nums[left]) {
43-
right = mid - 1;
44-
} else {
45-
left++;
4+
public static class Solution1 {
5+
public int findMin(int[] nums) {
6+
int left = 0;
7+
int right = nums.length - 1;
8+
if (nums[left] < nums[right]) {
9+
return nums[left];
10+
}
11+
int min = nums[0];
12+
while (left + 1 < right) {
13+
int mid = left + (right - left) / 2;
14+
min = Math.min(min, nums[mid]);
15+
if (nums[mid] > nums[left]) {
16+
min = Math.min(nums[left], min);
17+
left = mid + 1;
18+
} else if (nums[mid] < nums[left]) {
19+
right = mid - 1;
20+
} else {
21+
left++;
22+
}
23+
}
24+
min = Math.min(min, Math.min(nums[left], nums[right]));
25+
return min;
4626
}
47-
}
48-
min = Math.min(min, Math.min(nums[left], nums[right]));
49-
return min;
5027
}
51-
}
5228
}

0 commit comments

Comments
 (0)