leetcode 229 Majority Element II

这题用到的基本算法是Boyer–Moore majority vote algorithm

wiki里有示例代码

 1 import java.util.*;
 2 public class MajorityVote {
 3     public int majorityElement(int[] num) {
 4         int n = num.length;
 5         int candidate = num[0], counter = 0;
 6         for (int i : num) {
 7             if (counter == 0) {
 8                 candidate = i;
 9                 counter = 1;
10             } else {
11                 if (i == candidate) {
12                     counter++;
13                 } else {
14                     counter--;
15                 }
16             }
17         }
18 
19         counter = 0;
20         for (int i : num) {
21             if (i == candidate) counter++;
22         }
23         if (counter < (n + 1) / 2) return -1;
24         return candidate;
25 
26     }
27     public static void main(String[] args) {
28         MajorityVote s = new MajorityVote();
29         System.out.format("%d\n", s.majorityElement(new int[] {1, 2, 3}));
30         System.out.format("%d\n", s.majorityElement(new int[] {2, 2, 3}));
31     }
32 }

  基本想法是这样的:在数组中数目超过 n /2的元素至多有一个,所以遍历过程中只有一个候选元素

我们假设有某个元素满足这种要求:若他均匀分布,至少每间隔一个出现一次;而且还不够,至少在某处多出现了一次

现在想一下不均匀的情况:如果该元素间隔了很长没出现,则至少在这个长间隔的前面或后面出现密集区域。

vector<int> majorityElement(vector<int>& nums) {
    if(nums.empty())
        return vector<int>();
    if(nums.size() == 1){
        return vector<int>({nums[0]});
    }
    vector<pair<int, int>> candidates;
    for(auto num : nums){
        if(candidates.size() < 1){
            candidates.emplace_back(num, 2);
        }
        else if(candidates.size() < 2){
            if(num != candidates[0].first)
                candidates.emplace_back(num, 2);
            else
                candidates[0].second++;
        }
        else{
            if(num == candidates[0].first){
                candidates[0].second++;
                candidates[1].second--;
            }
            else if(num == candidates[1].first){
                candidates[1].second++;
                candidates[0].second--;
            }
            else{
                candidates[0].second--;
                candidates[1].second--;
                int ind = candidates[0].second < candidates[1].second ? 0 : 1;
                if(candidates[ind].second <= 0){
                    candidates[ind].first = num;
                    candidates[ind].second = 2;
                }
            }
        }
    }
    for(auto& candidate : candidates){
            candidate.second = 0;
    }
    for(auto num : nums){
        for(auto& candidate : candidates){
            if(num == candidate.first)
                candidate.second++;
        }
    }
    vector<int> result;
    for(auto candidate:candidates){
        if(candidate.second > nums.size() / 3)
            result.push_back(candidate.first);
    }
    return result;
}

  

转载于:https://www.cnblogs.com/hustxujinkang/p/4705734.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值