剑指Offer-题39(Java版):数组中出现次数超过一半的数字

参考自:《剑指Offer——名企面试官精讲典型编程题》

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

主要思路:数字出现的次数超过数组长度的一半,说明它出现的次数比其他所有数字出现的次数和还要多。因此,可以在遍历数组的时候,保存两个值:数组的某个数字A和次数。

  • 遍历时,若次数为0,则把A更新为当前数字,且把次数设为1;
  • 若当前数字等于A,则次数加1;
  • 若当前数字不等于A,则次数减1。
    由于要找的数字出现的次数比其他所有数字出现的次数之和还要多,那么,最后保存的次数肯定大于0,且要找的数字就是最后一次更新的数字A。

另外一种解法:出现次数超过数组长度一半的数字,一定是数组的中位数。因此,可使用快速排序中的切分方法,找到把数组切分成两半的数字,即为所求数字。

关键点:输入数组特点,快速排序切分

时间复杂度:O(n)

public class MoreThanHalfNumber {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 2, 2, 2, 5, 4, 2};
        int result = findCountMoreThanHalfAll(array);
        //int result = findCountByPartition(array);
        System.out.println(result);
    }

    private static int findCountMoreThanHalfAll(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int count = 1;
        int currentValue = array[0];
        for (int i = 1; i < array.length; i++) {
            //更新保存的数
            if (count == 0) {
                currentValue = array[i];
                count = 1;
            } else if (currentValue == array[i]) {
                //与保存的数相等,则加1
                count++;
            } else {
                //与保存的数不相等,则减1
                count--;
            }
        }
        if (!checkIsValid(array, currentValue)) {
            currentValue = 0;
        }
        return currentValue;
    }

    //快速排序中的切分方法
    private static int findCountByPartition(int[] data) {
        int left = 0;
        int right = data.length - 1;
        int index = partition(data, left, right);
        int middle = data.length / 2;
        //找到把数组切分成两半的数字
        while (index != middle) {
            // 继续切分左边
            if (index > middle) {
                right = index - 1;
                index = partition(data, left, right);
            } else {
                // 切分右边
                left = index + 1;
                index = partition(data, left, right);
            }
        }
        //返回中位数
        return data[middle];
    }

    private static int partition(int[] data, int left, int right) {
        if (left >= right) {
            return right;
        }
        int base = data[left];
        int runLeft = left + 1;
        int runRight = right;
        while (true) {
            //防止数组越界
            while (data[runLeft] <= base && runLeft < right) {
                runLeft++;
            }
            while (data[runRight] > base && runRight > left) {
                runRight--;
            }
            if (runLeft >= runRight) {
                break;
            }
            swap(data, runLeft, runRight);
        }
        swap(data, left, runRight);
        return runRight;
    }

    private static void swap(int[] data, int a, int b) {
        int temp = data[a];
        data[a] = data[b];
        data[b] = temp;
    }

    /**
     * 检查结果出现的次数是否大于数组长度的一半
     *
     * @param array  the array
     * @param result the result
     * @return the boolean
     */
    private static boolean checkIsValid(int[] array, int result) {
        int count = 0;
        for (int i : array) {
            if (i == result) {
                count++;
            }
        }
        return count * 2 > array.length;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值