-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy path153.寻找旋转排序数组中的最小值.java
More file actions
55 lines (51 loc) · 1.05 KB
/
153.寻找旋转排序数组中的最小值.java
File metadata and controls
55 lines (51 loc) · 1.05 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
54
/*
* @lc app=leetcode.cn id=153 lang=java
*
* [153] 寻找旋转排序数组中的最小值
*
* https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/
*
* algorithms
* Medium (50.89%)
* Likes: 209
* Dislikes: 0
* Total Accepted: 58.9K
* Total Submissions: 115.4K
* Testcase Example: '[3,4,5,1,2]'
*
* 假设按照升序排序的数组在预先未知的某个点上进行了旋转。
*
* ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
*
* 请找出其中最小的元素。
*
* 你可以假设数组中不存在重复元素。
*
* 示例 1:
*
* 输入: [3,4,5,1,2]
* 输出: 1
*
* 示例 2:
*
* 输入: [4,5,6,7,0,1,2]
* 输出: 0
*
*/
// @lc code=start
class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left];
}
}
// @lc code=end