-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution154.java
40 lines (33 loc) · 1.01 KB
/
Solution154.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package leetcode.binarysearch;
public class Solution154 {
public static void main(String[] args) {
// int[] nums = new int[]{3, 1, 3};
int[] nums = new int[]{1, 3, 5};
// int[] nums = new int[]{2, 2, 2, 0, 1};
// int[] nums = new int[]{3, 3, 1, 3};
// int[] nums = new int[]{3, 1, 1};
System.out.println(new Solution154().findMin(nums));
}
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + right >> 1;
if (nums[mid] > nums[right]) {
left = mid + 1;
continue;
}
if (nums[mid] < nums[right]) {
right = mid;
continue;
}
int min = nums[mid];
for (int i = left; i <= right; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
return min;
}
return -1;
}
}