The sword refers to offer—the smallest number in the rotation array

Topic description
Moving the first elements of an array to the end of the array is called the rotation of the array. Input a rotation of a non-decreasingly sorted array, output 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. NOTE: All elements given are greater than 0, if the array size is 0, please return 0.
topic ideas
The minimum value after the non-decreasing array is rotated, that is, to find the dividing point. Before and after the dividing point, there are non-decreasing arrays. The non-decreasing array after the dividing point is smaller than the array before the dividing point. Therefore, the rotated array is searched in order. If a number is one hour earlier than the previous one, this number is the minimum value. If there is no situation where the latter number is smaller than the previous number, it means that all numbers in the array are equal, and the first number in the array can be returned. Note that considering the case that the array is empty, return 0
code
import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if(array.length<=0){
            return 0;
        }
        for(int i=0;i<array.length-1;i++){
            if(array[i]>array[i+1]){
                return array[i+1];
            }
        }
        return array[0];
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326629850&siteId=291194637