LeetCode--Majority Element II & 多数投票算法

题目

Given an integer array of size n, find all elements that appear more
than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in
O(1) space.

相似题目及多数投票算法

第一眼看到这个题,相信许多人都恩给你联想到另一个题目:

给定一个整数数组,找出出现次数大于 n/2 的那个数。

如果是找出出现次数大于n/2的数,解决这个问题的思路并不难,可以用Map扫描一遍,并且统计出现次数。但是这种方法的时间复杂度虽然是O(N),空间复杂度也相应的是O(N)。还有一种方法是多数投票算法,算法的名字虽然没听说过,但是思路相信大多数人还是知道的。

  1. 如果count==0,则将now的值设置为数组的当前元素,将count赋值为1;
  2. 反之,如果now和现在数组元素值相同,则count++,反之count–;
  3. 重复上述两步,直到扫描完数组。

之前做过的题目都是确定数组中有大于n/2的那个数存在的,所以没有了后边的检查步骤。

思路

看到这题首先想到的是出现次数大于⌊ n/3 ⌋的数不只有一个,思考了一下,这个数最多有两个,最少一个也没有。所以可以用一个大小为2的数组保存候选结果,最后进行筛选。

然后是候选结果的保存,考察答题者的知识迁移能力,可怜的是我迁移了半天也没移对-,-

这里不说我的错误思路了。直接说解题方法吧。

以nums = [8,8,7,7,7]为例

nums中当前遍历的数候选数组count数组
8[8 , 0 ][1 , 0]
8[8 , 0 ][2 , 0]
7[8 , 7 ][2 , 1]
7[8 , 7 ][2 , 2]
7[8 , 7 ][2 , 3]

一行跟第二行的0是默认初始化的值

代码

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> result = new ArrayList<Integer>();
        if(nums == null || nums.length == 0){
            return result;
        }
        int[] numbers = new int[2];
        int[] count = new int[2];

        for(int i = 0 ; i < nums.length ; i++){
            if(count[0] == 0){
                numbers[0] = nums[i];
            }else if(count[1] == 0 && numbers[0] != nums[i]){
                numbers[1] = nums[i];
            }

            if(numbers[0] == nums[i]){
                count[0]++;
            }else if(numbers[1] == nums[i]){
                count[1]++;
            }else{
                count[0]--;
                count[1]--;
            }
        }

        count[0] = count[1] = 0;
        for(int i = 0 ; i < nums.length ; i++){
            for(int j = 0 ; j < numbers.length ; j++){
                if(nums[i] == numbers[j]){
                    count[j]++;
                }
            }
        }

        if(count[0] > nums.length / 3)
            result.add(numbers[0]);
        if(count[1] > nums.length / 3 && numbers[1] != numbers[0])
            result.add(numbers[1]);


        return result;
    }
}

至于最后这一句

if(count[1] > nums.length / 3 && numbers[1] != numbers[0])

为什么要判断numbers[1] != numbers[0],是因为开始的时候初始化为0,要考虑都为0的情况。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值