algorithm-base/animation-simulation/二分查找及其变种/leetcode 81不完全有序查找目标元素(包含重复...

78 lines
3.6 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>进入。
## ****
![640](https://img-blog.csdnimg.cn/img_convert/9f77a33a7ff5b3fd8bbb98d77cb8a499.png)
使?
![](https://img-blog.csdnimg.cn/20210321134336356.png)
nums[left] == nums[mid] left ++ 13111 nums[mid] == nums[left] left ++, 3 left++
#### [81. II](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/)
#### ****
( [0,0,1,2,2,5,6] [2,5,6,0,0,1,2] )
true false
1
> nums = [2,5,6,0,0,1,2], target = 0 true
2
> nums = [2,5,6,0,0,1,2], target = 3 false
#### ****
nums[mid] == nums[left] left++退
#### ****
Java Code:
```java
class Solution {
public boolean search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left+((right-left)>>1);
if (nums[mid] == target) {
return true;
}
if (nums[mid] == nums[left]) {
left++;
continue;
}
if (nums[mid] > nums[left]) {
if (nums[mid] > target && target >= nums[left]) {
right = mid - 1;
} else if (target > nums[mid] || target < nums[left]) {
left = mid + 1;
}
}else if (nums[mid] < nums[left]) {
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1;
} else if (target < nums[mid] || target > nums[right]) {
right = mid - 1;
}
}
}
return false;
}
}
```