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.

Ideas:

思路与之前的一致,只是在遇到相同值时只能移动一步

while (start <= end) {
    int mid = start + (end-start)/2;
    if (nums[mid] == target) {
        return true;
    } else if (target < nums[mid]) {
        if (target < nums[start] && nums[start] < nums[mid]) { 
            start = mid + 1;
        } else if (nums[mid] == nums[start]) {
            start++; //相同时只能移一步
        } else {
            end = mid - 1;
        }
    } else {
        if (target > nums[end] && nums[end] > nums[mid]) { 
            end = mid - 1;
        } else if (nums[end] == nums[mid]) {
            end--; //相同时只能移一步
        } else {
            start = mid + 1;
        }
    }
}

results matching ""

    No results matching ""