LeetCode 229 Majority Element II (投票算法)

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

Example 1:

Input: nums = [3,2,3]
Output: [3]

Example 2:

Input: nums = [1]
Output: [1]

Example 3:

Input: nums = [1,2]
Output: [1,2]

Constraints:

  • 1 <= nums.length <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9

Follow up: Could you solve the problem in linear time and in O(1) space?

题目链接:https://leetcode.com/problems/majority-element-ii/

题目大意:求频数超过n/3的数,要求O(1)空间,O(n)时间

题目分析:求频数最高的两个数字,然后判断这两个数字的频数是否超过n/3,求频数最高的两个数可以通过投票算法,大致思路:

设两个数为n1,n2,选票的相对值分别为c1,c2,注意这里记录的是相对值,不是绝对的频数,相对值可以理解为如果某次当前数字未被选中,则票数减1,选中则加1。若c1或c2为0则说明当前不存在票数明显多(频数高)的数字,则可以直接取当前数字继续往后累计票数

1ms,时间击败99.88%

class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> ans = new ArrayList<>();
        int c1 = 1, n1 = nums[0], c2 = 0, n2 = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == n1) {
                c1++;
                continue;
            }
            if (nums[i] == n2) {
                c2++;
                continue;
            }
            if (c1 == 0) {
                n1 = nums[i];
                c1 = 1;
                continue;
            }
            if (c2 == 0) {
                n2 = nums[i];
                c2 = 1;
                continue;
            }
            c1--;
            c2--;
        }
        c1 = 0;
        c2 = 0;
        for (int num : nums) {
            if (n1 == num) {
                c1++;
            } else if (n2 == num) {
                c2++;
            }
        }
        int minFreq = nums.length / 3; 
        if (c1 > minFreq) {
            ans.add(n1);
        }
        if (c2 > minFreq) {
            ans.add(n2);
        }
        return ans;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值