Find minimum in rotated sorted array II

Source: Internet
Author: User

Follow upFor "find minimum in rotated sorted array ":
What ifDuplicatesAre allowed?

Wocould this affect the run-time complexity? How and why?

Suppose a sorted array is rotated at some unknown to you beforehand.

(I. e .,0 1 2 4 5 6 7Might become4 5 6 7 0 1 2).

Find the minimum element.

The array may contain in duplicates.

Answer
public class Solution {    int []num;    public int findMin(int start,int end)    {        if(start==end)        {            return num[start];        }        if(num[start]==num[end])        {            return findMin(start+1,end);        }        int middle=start+((end-start)>>1);        if(num[middle]>num[end])        {            return findMin(middle+1,end);        }        return findMin(start,middle);    }    public int findMin(int[] num) {        this.num=num;        return findMin(0,num.length-1);    }}


Arraybinary search

Find minimum in 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.