"Sword means offer" face question 11. Minimum number of rotated array

Source: Internet
Author: User

Interview question 11. Minimum numeric title description of rotated array
    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。     NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
Answer

Complexity: O (logn) + O (1)

public class Solution {    public int minNumberInRotateArray(int [] array) {        if(array==null||array.length==0)            return 0;        int left = 0,right = array.length - 1;        while(left<right){            int mid = left + (right - left)/2;            if(array[mid]<=array[right]){                right = mid;            }else{                left = mid + 1;            }        }        return array[left];                /*if(array==null||array.length==0)            return 0;        Arrays.sort(array);        return array[0];        */    }}

"Sword means offer" face question 11. Minimum number of rotated 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.