Sword finger offer--face question 8: the smallest digital--java of the rotational array is realized __java

Source: Internet
Author: User

Topic Description:
Moving a number of the first elements of an array to the end of the array, we call it the rotation of the array. Enter a rotation for an incrementally 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 minimum value of the array is 1.

Ideas for solving problems:
1. Brute force solution, traverse it to find the smallest value, the complexity of O (n), but no l use the characteristics of the array
2. Two-point lookup. The array here is two ordered arrays, the binary lookup is very efficient for ordered arrays, and the complexity is O (LGN).

Analysis:
1. First analysis of the characteristics of the array, the two ordered arrays, the previous array of all values are greater than or equal to the value of the last array, the minimum value should appear in the first element of the latter array.

2. Two-point lookup narrowing the lookup range relies on a comparison of the intermediate value and the value to be looked for.
Set two pointers, Index1 point to the first element, index2 to the last element. If the middle Midindex points to an element that is greater than the index2 point, then the smallest element must be after midindex. If Midindex points to an element that is less than the index1 point, then the smallest element must be after midindex.
is to use this kind of judgment to keep narrowing the search scope.
3. Termination conditions:
Eventually index1 points to the last element of the previous array, and INDEX2 points to the first element of the latter array, between which the distance is 1. And Index2 is pointing to the smallest element.

4. Special Circumstances:
If the number of rotations is 0, which is an array of ascending order, there is no need to look, because the first element is the smallest element.

There is also a situation often encountered in the lookup, that is, the case of the same element.
For example {1, 0, 1, 1, 1} is a rotation of {0, 1, 1, 1, 1}
Index1, Index2, Midindex point to the element values are 1, at this time can not determine the minimum value of the range, so need sequential traversal.

Although the idea is relatively simple, but I feel that there are some skills in writing code

Package com.java.offer; public class Reversemin {public static int min (int[] numbers, int length) throws Exception {if (numbers = = null | |
        Length <= 0) {throw new Exception ("Invalid parameters");
        int index1 = 0;

        int index2 = length-1;
        Initialized to Index1 is to determine the number of rotations of 0 of the case int midindex = index1; 
                while (Numbers[index1] >= Numbers[index2]) {if (index2-index1 = = 1) {midindex = Index2;
            Break

            } Midindex = (index1 + index2)/2; If three values are equal, there is no way to judge in that interval if (numbers[index1] = = Numbers[midindex] && numbers[midindex

            ] = = = Numbers[index2]) {return Mininorder (numbers, index1, index2);
            } if (Numbers[midindex] >= numbers[index1]) {index1 = Midindex; else if (Numbers[midindex] < Numbers[index2]) {Index2 = midIndex;
    }//Can not return NUMBER[INDEX2], also take into account the number of rotations in the case of 0 returns NUMBERS[MIDINDEX];
        private static int Mininorder (int[] numbers, int index1, int index2) {Int. result = Numbers[index1]; for (int i = index1 + 1; I <= index2, i++) {if (Result > Numbers[i)) {result = Numb
            Ers[i];
    } return result;
        public static void Main (string[] args) {int[] a = {3, 4, 5, 1, 1, 2};
        Int[] B = {1, 0, 1, 1, 1};
        Int[] c = {};
            try {System.out.println (min (A, a.length));
            System.out.println (min (b, b.length));
        System.out.println (min (c, c.length));
        catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
 }

    }

}

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.