Lintcode: Finding the minimum value in a rotated sorted array II

Source: Internet
Author: User
Tags lintcode

look for the minimum value in the rotated sorted array II

Suppose a rotated sorted array whose starting position is unknown (e.g. 0 1 2 4 5 6 7 May become 4 56 7 0 1 2).

You need to find the smallest of these elements.

There may be duplicate elements in the array.

Solving

Violence Direct Linear Lookup

Alternatively, linear finds the number corresponding to the first starting descending position

The dichotomy should be considered

Recursion + two points

 Public classSolution {/**     * @paramnum:a rotated sorted array *@return: The minimum number in the array*/     Public intFindmin (int[] num) {        //Write your code here        if(num = =NULL|| Num.length = = 0)            return0; if(Num.length = = 1)            returnNum[0]; returnFindmin (num,0,num.length-1); }     Public intFindmin (int[] num,intLeftintRight ) {        if(left = =Right )returnNum[left]; if(right = = left + 1)            returnmath.min (Num[left],num[right]); intMid = (left + right)/2; if(Num[right] >Num[left])returnNum[left]; Else if(Num[right] = =num [left])returnFindmin (Num,left + 1, right); Else if(Num[mid] >=Num[left])returnfindmin (num,mid,right); Else             returnfindmin (Num,left,mid); }}
Java Code

Two points

 Public classSolution {/**     * @paramnum:a rotated sorted array *@return: The minimum number in the array*/     Public intFindmin (int[] num) {        //Write your code here        if(num = =NULL|| Num.length = = 0)            return0; if(Num.length = = 1)            returnNum[0]; intLow = 0; intHigh = Num.length-1; intMID =Low ;  while(Low <High ) {Mid= (low + high)/2; if(Num[mid] >Num[high]) { Low= Mid + 1; }Else if(Num[mid] <Num[high]) { High=mid; }Else{ High--; }        }        returnNum[low]; } }
Java Code

Lintcode: Finding the minimum value in a 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.