Skip to content

Commit 7f307b5

Browse files
author
ChienkuChen
committed
Add 153
1 parent 18383b6 commit 7f307b5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package _153;
2+
3+
class Solution {
4+
public int findMin(int[] nums) {
5+
int pivot = nums.length / 2;
6+
7+
while (pivot - 1 >= 0 && nums[pivot - 1] <= nums[pivot]) {
8+
pivot--;
9+
}
10+
11+
12+
if (pivot == 0) {
13+
int tmp = nums.length / 2;
14+
while (tmp + 1 < nums.length && nums[tmp] <= nums[tmp + 1]) {
15+
tmp++;
16+
}
17+
18+
return (tmp + 1) >= nums.length ? nums[0] : Math.min(nums[0], nums[tmp + 1]);
19+
} else
20+
return Math.min(nums[0], nums[pivot]);
21+
}
22+
23+
public static void main(String[] args) {
24+
System.out.println(new Solution().findMin(new int[]{1}));
25+
System.out.println(new Solution().findMin(new int[]{1, 2, 3}));
26+
System.out.println(new Solution().findMin(new int[]{7, 8, 4, 5, 6}));
27+
System.out.println(new Solution().findMin(new int[]{1, 8, 4, 5, 6}));
28+
System.out.println(new Solution().findMin(new int[]{3, 4, 5, 1, 2}));
29+
System.out.println(new Solution().findMin(new int[]{4, 5, 6, 7, 0, 1, 2}));
30+
}
31+
}

0 commit comments

Comments
 (0)