Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
53 lines (48 loc) · 1.69 KB

File metadata and controls

53 lines (48 loc) · 1.69 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.