154. Find Minimum in rotated Sorted Array II

Source: Internet
Author: User

    /** 154. Find Minimum in rotated Sorted Array II * 2016-6-1 by Mingyang * Originally we are dependent on the middle and edge elements of the size relationship, * to determine which half is unaffected by the rotate, still orderly     Of * And now because of the repetition, if we encounter the middle and edge of the same situation, * we can not judge which way is orderly, because which can be ordered.     Suppose the original array is {1,2,3,3,3,3,3}, * then it may be {3,3,3,3,3,1,2} after rotation, or {3,1,2,3,3,3,3}, so we can judge the left edge and center of the time is 3, we do not know which half should be cut off.     * The solution can only be to move the edge one step, until the edge and the middle is not equal or meet, which leads to the possibility that there will not be half cut.     * So the worst case scenario will occur each move one step, a total of moving n This, the time complexity of the algorithm becomes O (n). */     /** If found A.mid > A.left, indicate left is ordered, select left. * If found A.mid < A.left, indicating unordered state on the left, throw away the right * if A.mid = a.left, the description cannot be judged. Then we can put left++, discard one can. Don't worry about losing our target value.      Because a.left = = A.mid, even if you choose left, there is mid!      * Every time we go into the loop, we have to Judge A.left < A.right, because when we discard some numbers in the front, it is possible that the remaining arrays are ordered, so we should return directly a.left! */     //The author only changed the two strokes on the basis of I, so this one is very good.      Public intFindMin1 (int[] nums) {            intlen=nums.length; if(len==0| | nums==NULL)                  return0; intLow=0; intHigh=len-1;  while(low<=High ) {                    if(nums[low]<Nums[high])returnNums[low]; intMid= (Low+high)/2; if(mid==low| | high==Low ) {                        returnNums[low]<nums[high]?Nums[low]:nums[high]; }Else if(nums[mid]<nums[mid-1]&&nums[mid]<nums[mid+1]){                        returnNums[mid]; }Else if(nums[low]<nums[mid]&&nums[high]<Nums[mid]) { Low=mid+1; }Else if(nums[low]<nums[mid]&&nums[mid]<Nums[high]) { High=mid-1; }Else if(nums[low]==Nums[mid]) { Low++;//here is the situation of more than 1, if low==mid, do not low=mid+1, or can not live 311}Else{ High--;//This is a lot of things. 2                    }                }                returnNums[low]; }

154. 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.