lintcode-finding the minimum value in a rotated sorted array Java needs review

Source: Internet
Author: User
Tags lintcode

Title Description:

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.

You can assume that there are no duplicate elements in the array.

Code implementation:

 Public classSolution {/** @param nums:a rotated sorted array * @return: The minimum number in the array*/     Public intFindmin (int[] nums) {        if(nums==NULL|| Nums.length==0){            return-1; }        intStart = 0; intEnd = Nums.length-1; inttarget =Nums[start];  while(Start+1 <end) {                        intMid = (start + end)/2; if(Nums[mid] >=target) {Start=mid; }            Else{End=mid; }        }                if(Nums[end] <=target) {            returnNums[end]; }Else{            returnTarget; }    }}

Note: Binary lookup, take the first element as target, then binary search, intermediate element and target comparison, the final

1. If the array is in the normal order then nums[end] must be greater than target, then the first element is the minimum value of the target;

2. If the array is not in normal order, then nums[end] must be less than the first number;

3. If the array is not in normal order, then Nums[start] becomes the maximum value of the preceding sequence, nums[end] becomes the minimum value of the subsequent sequence.

lintcode-finding the minimum value in a rotated sorted array Java needs review

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.