algorithm-base/animation-simulation/二分查找及其变种/leetcode153搜索旋转数组的最小值.md

107 lines
3.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
## ****
leetcode33
![](https://img-blog.csdnimg.cn/20210321134701939.png)
![](https://img-blog.csdnimg.cn/2021032113472644.png)
- nums[left] < nums[right] nums[left]
- left mid leftleft = mid + 1
- left mid left mid right right = mid right = mid - 1 mid right = mid
![](https://img-blog.csdnimg.cn/20210321134748668.png)
#### [153. ](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/)
#### ****
[0,1,2,4,5,6,7] [4,5,6,7,0,1,2]
1
> nums = [3,4,5,1,2]1
2
> nums = [4,5,6,7,0,1,2] 0
3
> nums = [1] :1
#### ****
[5,6,7,0,1,2,3]
![](https://img-blog.csdnimg.cn/20210321134814233.png)
****
Java Code:
```java
class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
if (nums[left] < nums[right]) {
return nums[left];
}
int mid = left + ((right - left) >> 1);
if (nums[left] > nums[mid]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left];
}
}
```
C++ Code:
```cpp
class Solution {
public:
int findMin(vector <int> & nums) {
int left = 0;
int right = nums.size() - 1;
while (left < right) {
if (nums[left] < nums[right]) {
return nums[left];
}
int mid = left + ((right - left) >> 1);
if (nums[left] > nums[mid]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left];
}
};
```