这题用到的基本算法是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. DI 之 3.3 更多DI的知识(陆)

    3.3.1  延迟初始化Bean 延迟初始化也叫做惰性初始化,指不提前初始化Bean,而是只有在真正使用时才创建及初始化Bean. 配置方式很简单只需在<bean>标签上指定 " ...

  2. GitHub学习资料

    GitHub账户注册注册了有一年多了(Joined on 13 Apr 2015),一直以来都是本地命令行上传到内网的Git服务器Gitlab.最近正好在学习新的编程语言,所以当初荒废的GitHub想 ...

  3. C++编程中const和#define的区别

    (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使用.(2) 类型和安全检查不同 define宏没有类型,不做任何类型检查,仅仅是展开. const常量有具 ...

  4. RAID5和RAID10,哪种RAID更适合你(上)

    [IT168 专稿]存储是目前IT产业发展的一大热点,而RAID技术是构造高性能.海量存储的基础技术,也是构建网络存储的基础技术.专家认为,磁盘阵列的性能优势得益于磁盘运行的并行性,提高设备运行并行度 ...

  5. backbonejs中的模型篇(一)

    一:模型及属性 模型是MVC应用的基石,它负责存放应用所需的数据,对数据的验证,执行访问控制,以及实现应用所需的特定业务逻辑. backbone通过扩展Backbone.Model对象来定义一个模型. ...

  6. comboBox的多选框之疑难杂症——逗号篇

    提笔写正文之前,首先要再次提醒一下自己,因为总是记不住,以至大神同事们都开始用“嫌弃”的眼光看自己了——遇到问题,自己去解决,没有什么问题是解决不掉的,不要在没认真努力思考之前就去麻烦大神同事,切记切 ...

  7. js模拟快捷键操作表单

    <html> <head> </head> <body> <script> //键盘快捷键提交表单ctrl+s document.onkey ...

  8. eclipse快捷键失效的解决办法

    今天敲html代码,突然发现ctrl+D不能用了,shift+enter/shift+ctrl+enter也不能用了,上网上搜了下,原来我是在文本模式下打开的.切换为html editor打开就o了. ...

  9. QPS

    你想建设一个能承受500万PV/每天的网站吗? 博客分类: 移动行业 PV  转自:http://elf8848.iteye.com/blog/967049 你想建设一个能承受500万PV/每天的网站 ...

  10. 动态链接库dll键盘钩子后台记录代码示例

    //.header #ifndef _DLLHOOK_H_ #define _DLLHOOK_H_ #include <windows.h> #define DLL_EXPORT_FUN ...