这题用到的基本算法是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;
}

  

leetcode 229 Majority Element II的更多相关文章

  1. [LeetCode] 229. Majority Element II 多数元素 II

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

  2. LeetCode 229. Majority Element II (众数之二)

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

  3. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...

  4. Java for LeetCode 229 Majority Element II

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

  5. (medium)LeetCode 229.Majority Element II

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

  6. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  7. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  8. 【刷题-LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  9. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

随机推荐

  1. 静态类和静态类成员(C# 编程指南)

    静态类与非静态类基本相同,但存在一个区别:静态类不能实例化. 也就是说,不能使用 new 关键字创建静态类类型的变量. 因为没有实例变量,所以要使用类名本身访问静态类的成员. 例如,如果名为 Util ...

  2. 最最最简单的轮播图(JQuery)

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. robotframework笔记10

    循环和条件 for循环 *** Settings *** Library BuiltIn Library Collections *** Test Cases *** TestCase01 My Ke ...

  4. 纯css3代码写下拉菜单效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 《Java程序设计》第二周学习总结

    20145224陈颢文<Java程序设计>第二周学习总结 教材学习内容总结 一.类型.变量与运算符 1.类型 整数: 可细分为为short整数(占2字节),int整数(占4字节),long ...

  6. python中的类变量、实例变量

    类变量,是各个实例共享的资源,就像中央空调,只有一个,但每个房间(实例)均可享用. 实例变量,是每个实例各自分配使用的变量,每个房间(实例)都有一台空调,供自己使用. class handle(obj ...

  7. Oracle 表死锁 解决

    问题:更新的Update语句一直在更新 卡在执行update语句的地方. 清除的方法: Oracle表死锁解除   我是在plsql中处理  1.先查询  select * from v$locked ...

  8. 多条查询sql语句返回多表数据集

    + + "';SELECT ProductID,ProductTitle,ProductName,SalePrice,ListingPrice,MainPicture,SaledItemCo ...

  9. 数据库索引<一> 索引结构表结构

    有很长时间没有更新博客了,再过几天都2月分了,如果再不更新一篇,我1月分都没有更新,保持连续,今天更新一篇. 最近没有什么看技术方面的东西,游戏,画画搞这些去了.我发现我每年一到年底就是搞这些东西,其 ...

  10. 转 velocity 模板使用总结

    Velocity是一个基于java的模板引擎.它允许任何人仅仅简单的使用模板语言来引用由java代码定义的对象. 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一 ...