-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMinRotatedSortArray_154.java
More file actions
53 lines (48 loc) · 1.69 KB
/
FindMinRotatedSortArray_154.java
File metadata and controls
53 lines (48 loc) · 1.69 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
package LeetCodeOJ;
/**
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element.
* The array may contain duplicates.
*
*/
public class FindMinRotatedSortArray_154 {
/**
* find the minimum of the array,but there are duplicate element
*
* @param nums
* @return
*/
public int findMin(int[] nums) {
int low = 0;
int high = nums.length - 1;
while (low < high) {
// 因为不重复所以 第一步判断起码有两个元素,
// 否则一定是判断< ,没有=
if (nums[low] < nums[high]) {
return nums[low];
} else {
// low > high :说明在某一点翻转了数组
int mid = low + (high - low) / 2;
// 如果中间的比最右边的还大,那么最小的肯定在mid+1 - high
// 否则 肯定在low,mid
if (nums[mid] > nums[high]) {
low = mid + 1;
} else if (nums[mid] < nums[high]) {
high = mid;
} else {
// 如果中间值与high相等,那么high-1即可
high--;
}
/*
* if(nums[low] < nums[mid]){ low = mid + 1; }else if ( > ){
* high = mid; }else{ low +=1; }
*/
}
}
return nums[low];
}
public static void main(String[] args) {
int[] nums = { 7, 8, 9, 9, 9, 10, 2, 2, 2, 3, 4, 4, 5 }; // {1}
System.out.println(new FindMinRotatedSortArray_154().findMin(nums));
}
}