Sword means offer------array, dichotomy---minimum number of rotated arrays

Source: Internet
Author: User

https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159&rp=1&ru=/ta/ coding-interviews&qru=/ta/coding-interviews/question-ranking  Test instructions A non-descending array of two arrays, looking for the smallest number of arrays. Topic:  Moving a number of elements at the beginning of an array to the end of the array, which we call rotation.   Enter a rotation of an incrementing sorted array, outputting the smallest element of the rotated array.   For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the smallest element of the array is 1.   analysis Http://blog.csdn.net/jacob_007/article/details /52601847 loop method to write dichotomy 1. The decision condition is start+1 less than end,start=0, end=size-1 2. mid=start+ ((End-start) >>1)  3. If DATA[MID] satisfies the condition to return directly, if the data that satisfies the condition is start=mid on the right side of mid, the data that meets the criteria will be end=mid 4 on the left side of mid. Finally, because left and right are adjacent, it is possible to judge, and the final Judgment also includes two cases of Nums.length ==1 and 2.    Code Import Java.util.arraylist;public class Solution {    public int Minnumberinrotatearray (int [] nums) {        if (nums = = NULL | | nums.length = = 0) Return 0;                int left = 0;&NBSP;&NBSP;&NBsp;     int right = Nums.length-1;        int MID = Left;                while (left + 1 < right) {            mid = left + (right-left)/2;             if (Nums[mid] = = Nums[left] && Nums[mid] = = nums[ Right]) return threeposequal (nums,left,right);             if (Nums[left] <= nums[mid]) left = mid;             else if (Nums[right] >= nums[mid]) right = mid;        }                 if (Nums[left] < nums[ Right]) return Nums[left];      &nbsP; else return Nums[right];    }        public int threeposequal (int[] Nums,int left,int right) {        int min = Nums[left] ;         for (int i = left + 1, left <= right; left++) {   &nbs P;        if (Nums[left] < min) min = nums[left];         }        return min;    }}

Sword refers to offer------array, dichotomy---the smallest number of rotated arrays

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.