LeetCode Search In Rotated Sorted Array 2


Follow up for “Search in Rotated Sorted Array”:
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

The original problem is here.

The original code is here.

My Solution

I solve this problem in C++, as below:

*Search in Rotated Sorted Array II
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
using namespace std;

class Solution {
    bool search(vector<int>& nums, int target) {
        int size = nums.size();
        if(size <= 0)
            return false;
        int start, end, middle;
        start = 0; 
        end = size-1;
        while(start <= end){
            middle = (start+end) / 2;
            if(nums[middle] == target){
                return true;
            else if(nums[middle] > target)
                end = middle - 1;
                start = middle + 1;
        return false;


To solve the problem, binary search is used.