-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMinimuminRotatedSortedArray.java
More file actions
49 lines (49 loc) · 1.28 KB
/
FindMinimuminRotatedSortedArray.java
File metadata and controls
49 lines (49 loc) · 1.28 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
package medium;
/**
* ClassName: FindMinimuminRotatedSortedArray.java
* Auther: chenyiAlone
* Create Time: 2019/4/29 19:02
* Description: No.153
* 思路:
* 1. 因为这种情况没有重复,所以可以直接归类为两种情况
* 1). 落在了旋转中心的左边
* 2). 落在了旋转中心的右边
* 2. 使用 res init Integer.MIN_VALUE 方法 mid 的左边小于 nums[mid] 的元素不存在
*
*
*
* Suppose an array sorted in ascending order 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.
*
* Example 1:
*
* Input: [3,4,5,1,2]
* Output: 1
* Example 2:
*
* Input: [4,5,6,7,0,1,2]
* Output: 0
*
*/
public class FindMinimuminRotatedSortedArray {
public int findMin(int[] nums) {
int lo = 0, hi = nums.length - 1;
int res = Integer.MAX_VALUE;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
int lv = nums[lo], mv = nums[mid], rv = nums[hi];
if (mv < lv || mv < rv) {
hi = mid - 1;
} else {
lo = mid + 1;
}
res = Math.min(res, mv);
}
return res;
}
}