# 81. Search in Rotated Sorted Array II

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/

> 内容描述

```
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:

Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:

This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******

在[leetcode第33题](https://github.com/apachecn/awesome-algorithm/blob/master/docs/Leetcode_Solutions/Python/033._search_in_rotated_sorted_array.md)
的基础上加2行就可以了

但是这样就可能会导致worst case变成O(N)，因为可能输入为[1,1,1,1,1,1,1]

beats 100%

```python
class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: bool
        """
        l, r = 0, len(nums) - 1
        while l <= r:
            while l < r and nums[l] == nums[r]: # 加上这两行就跟第33题一样了
                l += 1
            mid = l + ((r - l) >> 2)
            if nums[mid] == target:
                return True
            if nums[mid] <= nums[r]:
                if nums[mid] < target <= nums[r]:
                    l = mid + 1
                else:
                    r = mid - 1
            else:
                if nums[l] <= target < nums[mid]:
                    r = mid - 1
                else:
                    l = mid + 1
        return False
```


