Offer fluent wins the series - more than half of the number of times the number that appears array

Face questions 39: More than half of the number of times the number that appears array

One, Title Description

There are a number of array number that appears more than half the length of the array, find this number. Such as input 9 of an array of length {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.

Second, the problem analysis

We are most likely to think the idea is a digital than half the number, then the number after ordering the middle of an array of digital necessarily asked for.

Since it is an array, to involve sorting, they usually choose classic or random quick sort quick sort. Since the quick sort random basis from each array are divided randomly selected, so its influence reduced status data, time complexity is O (N * logN), rapid sorting condition of the affected data, time complexity difference then is O (n ^ 2). As used herein, it is quick sort, writing more convenient.

In fact, there is a more interesting, but also a very good idea of ​​the algorithm, is described as offensive and defensive positions ideas:

第一个数字作为第一个士兵,守阵地;count = 1;
遇到相同元素,count++;
遇到不相同元素,即为敌人,同归于尽,count--;当遇到count为0的情况,又以新的i值作为守阵地的士兵,继续下去,到最后还留在阵地上的士兵,有可能是主元素。
再加一次循环,记录这个士兵的个数看是否大于数组一般即可。

That is more than half the number of digital, then: the number of times the number that appears more than the sum of the numbers and other

Through the array stored during two values: a is a number in the array, the number of times the other. When traversing to the next number, if the same as the saved numbers, the number of plus 1, minus 1 and vice versa. If the frequency of 0, the next number is saved, the number is reset to 1. Due to the number of times looking for the number that appears more than the other figures and then looking for numbers certainly be the last time the number is set to the number 1.

Third, questions

A thought (not recommended)

    // 表示输入是否有效
    boolean isInputInvalid = true;
    // 快速排序
    public int MoreThanHalfNum(int [] array) {
        if(array==null ||array.length <= 0) {
            return 0;
        }
        int low=0;
        int high=array.length-1;
        int index=partition(array, low, high);

        while(index != array.length>>1){
            if(index < array.length>>1 ){
                low = index+1;
                index = partition(array,low,high);
            } else {
                high = index-1;
                index=partition(array,low,high);
            }
        }

        //判断次数是否超过一半
        int num=array[index];
        int times=0;
        for(int i=0;i < array.length;i++){
            if(array[i]==num){
                times++;
            }
        }
        if(times*2 > array.length){
            isInputInvalid=false;
            return num;
        }
        return 0;
    }

    private int partition(int[] array,int low ,int high){

        int pivotKey=array[low];
        while(low < high){
            while(low < high &&  array[high] >= pivotKey) {
                high--;
            }
            int temp=array[low];
            array[low]=array[high];
            array[high]=temp;
            while(low < high && array[low] <= pivotKey) {
                low++;
            }
            temp=array[low];
            array[low]=array[high];
            array[high]=temp;
        }
        return low;
    }

Thinking two (recommended)

	// 表示输入是否有效
    boolean isInputInvalid = true;

    public int MoreThanHalfNum(int [] array) {
        if(array==null || array.length <= 0) {
            return 0;
        }

        int num=array[0];
        int count = 1;
        for(int i=1;i<array.length;i++){
            if(count == 0) {
                num=array[i];
                count++;
            }
            else if(array[i] == num) {
                count++;
            }
            else {
                count--;
            }
        }
        
        if(count > 0){
            int times=0;
            for(int i=0;i<array.length;i++){
                if(array[i] == num){
                    times++;
                }
            }
            if(times*2 > array.length){
                isInputInvalid=false;
                return num;
            }
        }
        return 0;
    }
Published 158 original articles · won praise 3221 · Views 460,000 +

Guess you like

Origin blog.csdn.net/qq_42322103/article/details/104109490