Leetcode Find Minimum in rotated Sorted Array

Source: Internet
Author: User

An ordered array, without repeating numbers, pushing right several times to find the minimum value. For example

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

It doesn't make sense to go straight through once to find the answer. I've done a similar problem before, but I can't find a specific number. So just search the next garnker, know is search in rotated Sorted Array, the problem is to find the target value. Here is the minimum value to find.

I think so, divided into several parts,

1. If the value on the left is equal to the middle value, then the remaining two numbers are to be judged, and are adjacent.

2. If the middle value is greater than the value on the left, then the minimum value is definitely not on the leftmost side but only on the right.

3. If the median value is less than the value on the left, then the minimum value is not the middle value or the left piece.

classSolution { Public:    intFindmin (vector<int> &num) {        intLen = Num.size (), left =0, right = Len-1, Mid = (left + right)/2, Minmum =Int_max;  while(Left! =Right ) {Mid= (left + right)/2; if(Num[mid] = =Num[left]) {                if(Num[left] >Num[right]) left=Right ;  Break; }            Else if(Num[mid] >Num[left]) {                if(Num[left] < minmum) Minmum =Num[left]; Left= Mid +1; }            Else if(Num[mid] <Num[left]) {                if(Num[mid] < minmum) Minmum =Num[mid]; Right= Mid-1; }} minmum=min (minmum, num[left]);//This can't be forgotten.returnMinmum; }};

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.