"Leetcode" Find Minimum in rotated Sorted Array II Java implementation

Source: Internet
Author: User

I. Description of the topic

follow to "Find Minimum in rotated Sorted Array":
What if duplicates is allowed?

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

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.

The array may contain duplicates.

This title is the enhanced version of the find Minimum in rotated Sorted array, which adds duplicate elements to the array.

The concrete solution is analyzed in detail in http://www.cnblogs.com/rolly-yan/p/4032167.html, if necessary, please refer to.

Second, the Code implementation

Package Com.edu.leetcode;public class Findminimuminrotatedsortedarray {public int findmin (int[] num) {int start=0; int en D=num.length-1;while (Num[start]>=num[end]) {                      //num[start]>=num[end] indicates that the array is not in a positive order and the minimum value is not in the first position if (End-start ==1) {//Loop end condition when only two elements of return num[end];} int mid= (start+end)/2;if (Num[start]==num[mid]&&num[mid]==num[end]) {    //There are two conditions for this condition: 1, when there is only one element;//2, When there are a large number of duplicate elements in the array, you can no longer use the half-lookup int minvalue=num[start];for (int i=start+1;i<=end;i++) {if (num[i]<minvalue) minvalue= Num[i];} return minValue;} if (Num[mid]>=num[start]) {         //When this occurs, the minimum value appears between mid and end Start=mid;} else{//description minimum appears between start and mid End=mid;}} Return num[start];//This is a description of the array from start to end in a positive order, so the minimum value is the element of the start position}public static void main (string[] args) {//TODO Auto-generated method Stubfindminimuminrotatedsortedarray F = new Findminimuminrotatedsortedarray (); int[] num = {1}; SYSTEM.OUT.PRINTLN (F.findmin (num));}}

  

"Leetcode" Find Minimum in rotated Sorted Array II Java implementation

Related Article

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.