Search in Rotated Sorted Array II

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.

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        int lb = 0;
        int ub = nums.size() - 1;

        while(lb <= ub){
            int mid = (lb +ub)>>1;
            if(nums[mid] == target) return true;

            if(nums[lb] < nums[mid]){
                if(nums[lb] <= target && target < nums[mid]){
                    ub = mid - 1;
                }else{
                    lb = mid + 1;
                }
            }else if(nums[mid] < nums[lb]){
                if(nums[mid] < target && target <= nums[ub]){
                    lb = mid + 1;
                }else{
                    ub = mid - 1;
                }
            }else{
                lb++;
            }
        }

        return false;
    }
};

results matching ""

    No results matching ""