"154-find Minimum in rotated Sorted Array II (find the smallest number in the rotated Array II)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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.
Main Topic
Follow up on "Find array minimum for rotated sort": allows repeating elements, but two word groups are still locally ordered.
Thinking of solving problems
Searching using the class two search algorithm
Code Implementation
Algorithm implementation class
Public class solution { Public int Findmin(int[] nums) {//Parameter check if(Nums = =NULL|| Nums.length <1) {Throw NewIllegalArgumentException (); }intLo =0;inthi = Nums.length-1;intMID =0;//Can exclude the global order of the array while(Nums[lo] >= Nums[hi]) {//If there are only two elements, return the latter one if(Hi-lo = =1) {mid = Hi; Break; } mid = Lo + ((Hi-lo) >>1);if(Nums[mid] = = Nums[lo] && Nums[mid] = = Nums[hi]) {//can only use sequential search method, cannot adopt lo++,hi--way //Because Lo may be the last of a previous ordered array //Hi may also be the first of the latter an ordered array returnSequencesearch (Nums, lo, HI); }//If mid is in the previous ordered array if(Nums[mid] >= Nums[lo]) {lo = mid; }//If mid is in the next ordered array Else if(Nums[mid] <= Nums[hi]) {hi = mid; } }returnNums[mid]; }/** * The minimum value in the sequential search array, nums is rotated by an ordered array by an axis * * @param nums Search Array * @param start position * @param End position * @return min * / Public int Sequencesearch(int[] Nums,intStartintEnd) { for(inti = start; I < end; i++) {if(Nums[i] > nums[i +1]) {returnNums[i +1]; } }returnNums[start]; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47828189"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "155-find Minimum in rotated Sorted Array II (find the smallest number in the rotated Array II)"