剑指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])
            {
                count++;  //与保存的数相等,则加1
            } else
            {
                count--;  //与保存的数不相等,则减1
            }
        }
        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;
    }

    //检查结果出现的次数是否大于数组长度的一半
    private static boolean checkIsValid(int[] array, int result)
    {
        int count = 0;
        for (int i : array)
        {
            if (i == result) count++;
        }
        return count * 2 > array.length;
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37862405/article/details/80301669