Leetcode. Find Minimum in rotated Sorted Array

Source: Internet
Author: User

Suppose a sorted array is rotated on some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).

Find the minimum element.

Assume no duplicate exists in the array.

Array A,

1) If A[MID] < A[low], indicate minimum between [mid, Low]. (Presence rotated)

2) if A[MID] > A[low], there are two cases:

A) A[mid] > A[high], minimum between [Mid, High]. (Presence rotated)

b) A[mid] < A[high], minimum between [Low, mid]. (Rotated not present)

Notice termination condition: A[mid] Less than left or right side or low = = Mid

1 intFindmin (vector<int> &num)2     {3         intLow =0, high = Num.size ()-1, Mid =0;4          while(Low <=High )5         {6Mid = (low + high)/2;7             if(Mid = =0|| Num[mid] < Num[mid-1]) && (Mid = = Num.size ()-1|| Num[mid] < Num[mid +1]))8                  Break;9             if(Num[mid] <Num[low])Ten             { OneHigh = mid-1; A             } -             Else if(Num[mid] >Num[low]) -             { the                 if(Num[mid] >Num[high]) -Low = mid +1; -                 Else if(Num[mid] <Num[high]) -High = mid-1; +             } -             Else +             { A                 returnmin (Num[low], Num[high]); at             } -         } -          -         returnNum[mid]; -}

Leetcode. Find Minimum in rotated Sorted Array

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.