81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

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.

思路:此时可能存在nums[start]=nums[end]或者nums[start]=nums[mid]或者nums[mid]=nums[end]。所以无法用原来方法判断是否正序、右侧rotate、或者左侧rotate。解决方法是,当碰到nums[start]=nums[end]的情况时,end-1,寻找不同元素再进行二分法。

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        return binarySearch(nums,0,nums.size()-1, target);
    }

    bool binarySearch(vector<int>& nums, int start, int end, int target){
        if(start==end){
            if(nums[start]==target) return true;
            else return false;
        }

        if(nums[start]==nums[end]) return binarySearch(nums,start,end-1,target); //ignore duplicate

        int mid = start+ ((end-start)>>1);
        //正序
        if(nums[mid]>=nums[start] && nums[mid]<nums[end]){ //mid可能=start,所以>=
            if(target <= nums[mid]) return binarySearch(nums,start,mid,target); //mid肯定<end,所以至少舍弃了一个
            else return binarySearch(nums,mid+1,end,target); //mid+1,至少舍弃了一个
        }

        //右侧rotate
        else if(nums[mid]>=nums[start] && nums[mid]>=nums[end]){
            if(target>=nums[start] && target<=nums[mid]) return binarySearch(nums,start,mid,target);
            else return binarySearch(nums,mid+1,end,target);
        }

        //左侧rotate
        else{
            if(target>=nums[start] || target<=nums[mid]) return binarySearch(nums,start,mid,target);
            else return binarySearch(nums,mid+1,end,target);
        }
    }
};
时间: 2024-10-29 19:08:52

81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)的相关文章

33. Search in Rotated Sorted Array &amp;&amp; 81. Search in Rotated Sorted Array II &amp;&amp;

33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise

81. 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? Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4

leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索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. 思路:此题在解的时候,才发现Search in Rotated Sorted Array

【LeetCode】81. 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. 这题跟上一题一样,都是二分查找法,但是需要思考,对于时间复杂度,存在重复元素会有影

[leedcode 81] 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. public class Solution { public boolean search

[leetcode] 81. Search in Rotated Sorted Array II

class Solution { public: bool search(vector<int>& nums, int target) { int low=0; int high=nums.size()-1; //整个数组是分段递增的 while(low<high){ int mid=(low+high)/2; if(nums[mid]==target) return true; if(nums[mid]<nums[high]){//说明分界点在前半段,[mid,high]

81.Search in Rotated Sorted Array II---二分变形

题目链接 题目大意:与33题类似,只是这里数组中有重复数值. 法一:解法与33题类似,只是这里要处理1,3,1,1,1这种情况,即有重复值时,mid与left和right都相等时,可以采用right--的方式错开相等值再比较.代码如下(耗时2ms): 1 public boolean search(int[] nums, int target) { 2 int left = 0, right = nums.length - 1; 3 while(left <= right) { 4 int mi

[LeetCode] Search in Rotated Sorted Array I (33) &amp;&amp; II (81) 解题思路

33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise

33. Search in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic