To prove safety: more than half of the number of times the number that appears array

Title Description

There are a number of array number that appears more than half the length of the array, find this number. For example, a length of the input array 9  {1,2,3,2,2,2,5,4,2}. Since the number 2 appears five times, more than half the length of the array in the array, the output 2. If there is 0 output.

solution

A Solution

Using the partition idea of ​​fast row.

There are a number of array numbers appear more than half the length of the array, then sorted, in the middle of an array of digital necessarily the numbers we're looking for. We randomly choose a number, use partition () function that is smaller than the selected digital numbers are ranked in the left side of it, is greater than the selected digital numbers are ranked in the right side of it.

Analyzing the subscript numbers selected  index:

  • If  index = n/2, then this number is the median.
  • If  index > n/2, then followed by a partition in the left index.
  • If  index < n/2, then continued partition of the right index.

** Note: ** This method modifies the array input. The time complexity is  O(n).

 

public class Solution {
    
    public static int MoreThanHalfNum_Solution(int[] arr){
        if(arr == null || arr.length == 0){
            return 0;
        }
        int n=arr.length;
        int start = 0, end = n-1;
        int mid = n>>1;
        int index = partition(arr, start, end);
        while(index != mid){
            if(index > mid){
                end = index -1;
            }else{
                start = index +1;
            }
            index = partition(arr, start, end);
        }
        return isMoreThanHalf(arr, arr[index])?arr[index]:0;
    }
     

    private static boolean isMoreThanHalf(int[] arr, int val) {
        int cnt = 0;
        for(int e:arr){
            if(e == val){
                cnt++;    
            }
        }
        return cnt*2 > arr.length;
    }

    private static int partition(int[] arr, int start, int end) {
        int p = arr[end];
        while(start<end){
            while(start<end && arr[end]>=p) end--;
            arr[start] = arr[end];
            while(start<end && arr[start]<=p) start++;
            arr[end] = arr[start];
        }
        arr[start] = p;
        return start;
    }



    public static void main(String[] args) {
        int[] arr = {1,2,3,2,2,2,5,4,2};
        int num = MoreThanHalfNum_Solution(arr);
        System.out.println(num);
    }
}

 

 

Solution two

The use of majority voting algorithm, from start to finish through the array, encountered two different numbers put the two numbers simultaneously removed. Remove two numbers may not majority, it may be a majority the other is not, but because more than half the total number of majority, so it is deleted after the last remaining definitely majority.

This method is time complexity  O(n), and does not change the array.

public  class Solution {
     / ** 
     * lookups number appears more than half of the array 
     * 
     * @param Array array 
     * @return Returns the number does not exist, returns 0
      * / 
    public  int MoreThanHalfNum_Solution ( int [] Array) {
         IF (Array == null || be array.length == 0 ) {
             return 0 ; 
        } 
        
        int RES = Array [0 ];
         int Times =. 1 ;
         for ( int I =. 1; I <be array.length; ++ I) {
            IF (Times == 0 ) { 
                RES = Array [I]; 
                Times =. 1 ; 
            } the else  IF (Array [I] == RES) {
                 ++ Times; 
            } the else {
                 - Times; 
            } 
        } 

        return isMoreThanHalf (Array, RES) RES:? 0 ; 
    } 


    / ** 
     * val element really is determined whether more than half of the number of array elements 
     * 
     * @param array array 
     * @param val an element 
     * @return boolean
     */
    private boolean isMoreThanHalf(int[] array, int val) {
        int cnt = 0;
        for (int e : array) {
            if (e == val) {
                ++cnt;
            }
        }

        return cnt * 2 > array.length;
    }
}

 

Guess you like

Origin www.cnblogs.com/lisen10/p/11234202.html