# 81. Search in Rotated Sorted Array II

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

## 刷题内容
> 原题连接

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

> 内容描述

```
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
```

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

翻转数组一开始想到的就是用二分法，实现起来也不难，关键是判断 l = mid 还是 j = mid，不过这题还要单独考虑一下当nums[mid] == nums[l] 的情况，当nums[l] == nums[mid]时，分别查找[l,mid]和[mid,r]区间内是否存在target

```cpp
class Solution {
public:
    bool searchInternal( const vector<int>& Nums, const int Target, int Left, int Right )
    {
        if ( Right <= Left )
			return false;
        while ( Left < Right - 1 )
        {
            int Middle = (Left + Right) / 2;
            if ( Nums[Left] == Nums[Middle] )
            {
                return searchInternal( Nums, Target, Left, Middle )
                    || searchInternal( Nums, Target, Middle, Right );
            }
            if ( Target < Nums[Middle] )
            {
                if ( Nums[Left] < Nums[Middle] && Nums[Left] > Target )
                {
                    Left = Middle;
                }
                else
                {
                    Right = Middle;
                }
            }
            else if ( Target > Nums[Middle] )
            {
                if ( Nums[Left] > Nums[Middle] && Nums[Left] <= Target )
                {
                    Right = Middle;
                }
                else
                {
                    Left = Middle;
                }
            }
            else
            {
                return true;
            }
        }
        return Nums[Left] == Target;
    }
    bool search(vector<int>& nums, int target) {
        return searchInternal(nums,target,0,nums.size());
    }
};
```