Search in rotated Sorted Array II

Source: Internet
Author: User

Follow to "Search in rotated Sorted Array":
What if duplicates is allowed?

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

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


Solution

The subject can be in accordance with the Search in rotated Sorted array method, but you need to note that Nums[left]==nums[right] does not refer to the same element on behalf of the case, and contains nums[left]==nums[mid], such as 121111, which makes it impossible to tell which side of the mid is in order, is unable to know where the turning point is.


At this point, if nums[right] is also equal to Nums[mid], just need to left+=1,right-=1, because target must still be in left and right,

Other cases follow the search in rotated Sorted array method;

left=0,right=size-1;mid=left+ (Right-left)/2 It's best to write like this, right+left may overflow

  1. Nums[mid]==target return mid

  2. If Nums[left]==nums[mid]==nums[right] left++,right--;

  3. Nums[left]<=nums[mid] The equals sign exists here because left and mid may be the same subscript, in which case the turning point is on the right of mid and may contain mid

    A) if Nums[left]<=target&&target<nums[mid] is right=mid-1, this condition indicates that target is in a,

    b) does not satisfy a, the target is on the right side of mid, left=mid+1

  4. Nums[left]>nums[mid] In this case, the turning point is on the left of mid,

    A) if Nums[mid]<target&&nums[right]>=target is left=mid+1; In this case, the target is in B or, B is empty, A is ascending, (Mid,right],

    b) Not satisfying a, the target is on the left side of mid, natural right=mid-1


Step 3 can do this because, if Nums[left]<=nums[mid], and Nums[right]!=nums[mid], assuming that the turning point is not on the right side of mid, there is a turning point on the left of mid, Left and mid Direct Pos point to an element that is the maximum value, left-pos increments, pos+1->right increments, then right points to an element that is larger than mid, and is less than the left point, which points to the element size, rigt "mid" left, And by Nums[left]<=nums[mid] know, this contradiction, then 3 established, similarly, 4 was established.

int size=nums.size ();

if (size==0)

return false;

int left=0,right=size-1;

while (Left<=right) {

int mid=left+ (right-left)/2;//right+left may be overflow;

if (nums[mid]==target)

return true;

if (nums[left] = = Nums[mid] && Nums[mid] = = Nums[right])

{

++left;

--right;

}

else if (Nums[left]<=nums[mid]) {//contains equals, because left and mid May point to the same element,

if (Nums[left]<=target&&target<nums[mid]) {

Right=mid-1;

}

Else

left=mid+1;

}

else{

if (Nums[mid]<target&&target<=nums[right])

left=mid+1;

Else

Right=mid-1;

}

}

return false;

}



Solution Two:

This better understanding point,

To explain why, consider this sorted array 1111115 , which is rotated to 1151111 .

Assumeleft = 0andmid = 3, and theTargetWe want to search for is5. Therefore, the conditionA[left] == A[mid]Holds true, which leaves us with only BothPossibilities:

  1. All numbers between and is all A[left] A[right] 1 ' s.

  2. Different numbers (including our target) may exist between A[left] and A[right] .

as we cannot determine which of the above are true, the best we can do are to Move left  one Step to the right and repeat the process again. Therefore, we is able to construct a worst case input which runs in  O (n) , for Example:the Input 11111111...115 .

Below is a pretty concise code (thanks to Bridger) for your reference which I found from the old discuss.

BOOL Search (vector<int>& nums, int target) {

int size=nums.size ();

if (size==0)

return false;

int left=0,right=size-1;

while (Left<=right) {

int mid=left+ (right-left)/2;//right+left may be overflow;

if (nums[mid]==target)

return true;

else if (Nums[left]<nums[mid]) {//here contains Equals, [Left,mid] ordered,

if (Nums[left]<=target&&target<nums[mid]) {

Right=mid-1;

}

Else

left=mid+1;

}

else if (Nums[left]>nums[mid]) {//[mid,right] ordered

if (Nums[mid]<target&&target<=nums[right])

left=mid+1;

Else

Right=mid-1;

}

Else

left++;

}

return false;


Search in rotated Sorted Array II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.