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