剑指offer-29.数组中出现次数超过一半的数字

https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163?tpId=13&tqId=11181&tPage=2&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking

题目描述
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

题解:
方法一:
对原数组进行排序,如果存在出现次数超过数组长度一半的数字,那么这个数字一定在排序后的数组的中间位置。取出这个数字判断是否出现次数真的超过一半,否则不存在这样的数字,返回0。
这个时间复杂度为排序时间复杂度,为 O(nlogn)。

import java.util.Arrays;
public class Solution {
    public int MoreThanHalfNum_Solution(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        Arrays.sort(array);
        int result = array[array.length / 2];
        if (!checkMoreThanHalf(result, array)) {
            result = 0;
        }
        return result;
    }

    private boolean checkMoreThanHalf(int result, int[] array) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == result) {
                count++;
            }
        }
        if (count * 2 > array.length) {
            return true;
        } else {
            return false;
        }
    }
}

方法二:

受快排的启发,在快排中,我们先在数字中随机选择一个数字,然后调整数组中数字的顺序,使得比它小的数字都位于他的左边,比他大的数字都位于他的右边,如果这个选中的数字的下标刚好是 n/2 ,那么这个数字就是数组的中位数,如果它的下标大于 n/2 ,那么中位数位于它的左边,那么我们接着在他的左边寻找;如果它的下标小于n/2,那么数组的中位于一定位于它的右边,那么我们接着在他的右边寻找。这是一个递归过程。整个时间复杂度为 O(n).

public class Solution {
    int[] array;
    public int MoreThanHalfNum_Solution(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        this.array = array;
        int middle = array.length / 2;
        int start = 0;
        int end = array.length - 1;
        int index = quickGetPosition(array, start, end);
        while (index != middle) {
            if (index > middle) {
                end = index - 1;
                index = quickGetPosition(array, start, end);
            } else {
                start = index + 1;
                index = quickGetPosition(array, start, end);
            }
        }
        int result = array[middle];
        if (!checkMoreThanHalf(result, array)) {
            result = 0;
        }
        return result;
    }

    private int quickGetPosition(int[] array, int start, int end) {
        if (start < end) {
            int x = array[start];
            while (start < end) {
                while (start < end && array[end] > x) {
                    end--;
                }
                if (start < end) {
                    array[start] = array[end];
                    start++;
                }
                while (start < end && array[start] < x) {
                    start++;
                }
                if (start < end) {
                    array[end] = array[start];
                    end--;
                }
            }
            array[start] = x;
        }
        return start;
    }

    private boolean checkMoreThanHalf(int result, int[] array) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == result) {
                count++;
            }
        }
        if (count * 2 > array.length) {
            return true;
        } else {
            return false;
        }
    }
}

方法三:

数值中有一个数字出现的次数超过数组长度的一半,也就是说它出现的次数比其他所有数字出现次数的和还要多。我们可以遍历数组的时候保存两个值:一个是数组中的一个数字,一个是次数。当我们遍历到一个数字的时候,如果下一个数字和我们之前保存的相同,则次数加1 ;如果下一个数字和我们之前保存的数字不同,则次数减1。如果次数为零,我们需要保存下一个数字,并把次数设为1。由于我们要找的数字出现次数比其他数字出现次数之和都大,所以要找的数字肯定是最后一次把次数设为1时对应的数字。
时间复杂度为 O(n).

public class Solution {
    public int MoreThanHalfNum_Solution(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int result = array[0];
        int count = 1;
        for (int i = 1; i < array.length; i++) {
            if (count == 0) {
                result=array[i];
                count=1;
                continue;
            }
            if (array[i] == result) {
                count++;
            } else {
                count--;
            }
        }
        if(!checkMoreThanHalf(result, array)){
            result=0;
        }
        return result;
    }

    private boolean checkMoreThanHalf(int result, int[] array) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == result) {
                count++;
            }
        }
        if (count * 2 > array.length) {
            return true;
        } else {
            return false;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zxm1306192988/article/details/81076162