JAVA implementation of the minimum number problem of rotating arrays ("sword refers to offer") __java

Source: Internet
Author: User
Title: The smallest number of rotating arrays moves a number of the first elements of an array to the end of the array, which we call the rotation of the array. Enter a rotation for a non-descending sequence, 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 minimum value of the array is 1.
Screenshot of New Ket network problem:

The subject itself is very good, is a non-descending array (let's think of it as an incremental array of good) in front of the section and put it to the front (of course, you can not cut, that is, the length of the section is 0, and then run, loop, and judge the number of the first one larger than it and the latter one smaller than it, this number is the minimum number of the original series , which is the place where the previous position is grafted up after the back section is truncated. And then you're smart. Through the analysis will find that, as long as some simple pretreatment, you can greatly simplify the judgment conditions, of course, the conditions of their own judgment is not very complicated. Answer:
Import java.util.ArrayList;
public class Solution {public
    int minnumberinrotatearray (int [] array) {
         if (array==null| | Array.length==0) return 0;
        if (Array.Length ==1) return array[0];
        for (int j = 1;j<array.length; j + +) {
            if (Array[j]<array[j-1]) {return
                array[j];
            }
        }
        return array[0];
    }

New Ket Network compiled by screenshot:
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.