Minimum number of 06 digits rotation


The beginning of an array of several elements moved to the end of the array, the array we call rotation. A non-decreasing input array sort of a rotation, output rotation smallest element array. For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum. NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.

A thought: violence to solve

. 1  Import of java.util.ArrayList;
 2  Import Classes in java.util *. ;
 . 3  // title sorting said non-decreasing, it means that there may exist such a sequence 3,3,3,3,4,5,1,2
 4  // title no duplicate elements not specified 
. 5  public  class Solution {
 . 6      public  static  int minNumberInRotateArray ( int [] Array) {
 . 7          int length = be array.length;
 . 8          IF (length == 0) return 0 ;
 . 9          int min 0 = ;
 10          for ( int I = 0; I <length; I ++){
11             if(array[i+1] < array[i]){
12                 min= array[i+1];
13                 break;
14             }
15         }
16         if(min==0) min= array[0];
17         return min;
18     }
19     
20     public static void main(String [] args){
21         Scanner sc = new Scanner(System.in);
22         int temp = sc.nextInt();
23         ArrayList<Integer> inputdata = new ArrayList<Integer>();
24         while(sc.hasNext()){
25             inputdata.add(temp);
26         }
27         int [] array = new int[inputdata.size()];
28         for(int j=0;j<inputdata.size();j++){
29             array[j]= inputdata.get(j);
30         }
31         int result = minNumberInRotateArray(array);
32         System.out.println("旋转数组的最小数字是"+ result);
33     }
34 }

 

Thinking two: half the deformation find

mid = low + (high - low)/2
We need to consider three cases:
(1)array[mid] > array[high]:
This situation is similar to array [3,4,5,6,0,1,2], in this case a certain minimum number of mid right.
low = mid + 1
(2)array[mid] == array[high]:
This situation is similar to array [1,0,1,1,1] or [1,1,1,0,1], then the minimum number of bad judgment on the left mid
Or right, then I had a a try,
high = high - 1
(3)array[mid] < array[high]:
This situation is similar to array [2,2,3,4,5,6,6], then the minimum number of array necessarily [mid] or left mid
side. Because the right is inevitably increasing.
high = mid
 
Note that there is a pit: numerals forward if only the last two numbers range to be queried, then the mid certainly point to the next
For example array = [4,6]
array[low] = 4 ;array[mid] = 4 ; array[high] = 6 ;
If high = mid - 1, will generate an error, and therefore high = mid
However, the case (1), low = mid + 1 error will not
 
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        int low = 0 ; int high = array.length - 1;   
        while(low < high){
            int mid = low + (high - low) / 2;        
            if(array[mid] > array[high]){
                low = mid + 1;
            }else if(array[mid] == array[high]){
                high = high - 1;
            }else{
                high = mid;
            }   
        }
        return array[low];
    }
}

 

 

Guess you like

Origin www.cnblogs.com/shareidea94/p/11095711.html