Leetcode 153. 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.

Analysis:

This problem gives an ordered array to find the smallest element in the array, in fact, to find the first position less than the last element appears,

For example 4 5 6 0 1 2 given in the title,

The last element is 2,

Just find the location where the first element less than 2 appears.

The first element is not used as target, because there is a limit to flipping an array, which is to completely not flip, that is 0 1 4 5 6 7,

In this case, an error occurs if the first element is used as the target.

Find the first element that <= Targrt, and the last element is the target.

 Public classSolution {//find the first element that <= Targrt, and the last element is the target.     Public intFindmin (int[] nums) {        if(Nums.length = = 0){            return0; }        intStart = 0, end =nums.length; inttarget = Nums[nums.length-1]; intmid;  while(Start + 1 <end) {Mid= start + (End-start)/2; if(Nums[mid] <=target) {End=mid; }Else{Start=mid; }        }        if(Nums[start] <=target) {            returnNums[start]; }Else{            returnNums[end]; }    }}

Leetcode 153. 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.