forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findMinimumInRotatedSortedArray.java
46 lines (40 loc) · 1.29 KB
/
findMinimumInRotatedSortedArray.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
41
42
43
44
45
46
// Source : https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/
// Inspired by : https://www.jiuzhang.com/solutions/find-minimum-in-rotated-sorted-array/
// Author : Lei Cao
// Date : 2014-10-05
/**********************************************************************************
*
* 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.
*
* You may assume no duplicate exists in the array.
*
**********************************************************************************/
package findMinimumInRotatedSortedArray;
public class findMinimumInRotatedSortedArray {
public int findMin(int[] num) {
if (num == null || num.length == 0) {
return Integer.MIN_VALUE;
}
int start = 0;
int end = num.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (num[start] < num[end]) {
end = mid;
} else if (num[start] < num[mid]) {
start = mid;
} else {
end = mid;
}
}
if (num[start] < num[end]) {
return num[start];
} else {
return num[end];
}
}
}