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

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

练习地址

https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/

解法一:基于 Partition 函数的时间复杂度为 O(n) 的算法

在随机快速排序算法中,我们先在数组中随机选择一个数字,然后调整数组中数字的顺序,使得比选中的数字小的数字都排在它的左边,比选中的数字大的数字都排在它的右边。如果这个选中的数字的下标刚好是 n/2,那么这个数字就是数组的中位数;如果它的下标大于 n/2,那么中位数应该位于它的左边,我们可以接着在它的左边部分的数组中查找;如果它的下标小于 n/2,那么中位数应该位于它的右边,我们可以接着在它的右边部分的数组中查找。

public class Solution {
    public int MoreThanHalfNum_Solution(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int middle = array.length >> 1, start = 0, end = array.length - 1;
        int index = partition(array, start, end);
        while (index != middle) {
            if (index > middle) {
                end = index - 1;
            } else {
                start = index + 1;
            }
            index = partition(array, start, end);
        }
        int result = array[middle], times = 0;
        for (int number : array) {
            if (number == result) {
                times++;
            }
        }
        return times > array.length >> 1 ? result : 0;
    }
    
    // 基于《算法(第4版)》的快速排序算法代码
    private int partition(int[] a, int lo, int hi) {
        if (lo == hi) {
            return lo;
        }
        int i = lo, j = hi + 1;
        int v = a[lo];
        while (true) {
            while (a[++i] < v) {
                if (i == hi) {
                    break;
                }
            }
            while (v < a[--j]) {
                if (j == lo) {
                    break;
                }
            }
            if (i >= j) {
                break;
            }
            exch(a, i, j);
        }
        exch(a, lo, j);
        return j;
    }
    
    private void exch(int[] a, int i, int j) {
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
}

复杂度分析

  • 时间复杂度:O(n)。
  • 空间复杂度:O(1)。

解法二:根据数组特点找出时间复杂度为O(n)的算法

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

public class Solution {
    public int MoreThanHalfNum_Solution(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int result = 0, times = 0;
        for (int number : array) {
            if (times == 0) {
                result = number;
                times = 1;
            } else if (number == result) {
                times++;
            } else {
                times--;
            }
        }
        times = 0;
        for (int number : array) {
            if (number == result) {
                times++;
            }
        }
        return times > array.length >> 1 ? result : 0;
    }
}

复杂度分析

  • 时间复杂度:O(n)。
  • 空间复杂度:O(1)。

👉剑指Offer Java版目录
👉剑指Offer Java版专题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容