"Leetcode-Interview algorithm classic-java Implementation" "155-find Minimum in rotated Sorted Array II (find the smallest number in the rotated Array II)"

Source: Internet
Author: User

"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)"

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.