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

Note: The algorithm should run in linear time and in O(1) space.

Example 1:

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

Example 2:

Input: [1,1,1,3,3,2,2,2]
Output: [1,2]

这道题让我们求出现次数大于 n/3 的数字,而且限定了时间和空间复杂度,那么就不能排序,也不能使用 HashMap,这么苛刻的限制条件只有一种方法能解了,那就是摩尔投票法 Moore Voting,这种方法在之前那道题 Majority Element 中也使用了。题目中给了一条很重要的提示,让先考虑可能会有多少个这样的数字,经过举了很多例子分析得出,任意一个数组出现次数大于 n/3 的数最多有两个,具体的证明博主就不会了,博主也不是数学专业的(热心网友用手走路提供了证明:如果有超过两个,也就是至少三个数字满足“出现的次数大于 n/3”,那么就意味着数组里总共有超过 3*(n/3) = n 个数字,这与已知的数组大小矛盾,所以,只可能有两个或者更少)。那么有了这个信息,使用投票法的核心是找出两个候选数进行投票,需要两遍遍历,第一遍历找出两个候选数,第二遍遍历重新投票验证这两个候选数是否为符合题意的数即可,选候选数方法和前面那篇 Majority Element 一样,由于之前那题题目中限定了一定会有大多数存在,故而省略了验证候选众数的步骤,这道题却没有这种限定,即满足要求的大多数可能不存在,所以要有验证,参加代码如下:

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
int a = , b = , cnt1 = , cnt2 = , n = nums.size();
for (int num : nums) {
if (num == a) ++cnt1;
else if (num == b) ++cnt2;
else if (cnt1 == ) { a = num; cnt1 = ; }
else if (cnt2 == ) { b = num; cnt2 = ; }
else { --cnt1; --cnt2; }
}
cnt1 = cnt2 = ;
for (int num : nums) {
if (num == a) ++cnt1;
else if (num == b) ++cnt2;
}
if (cnt1 > n / ) res.push_back(a);
if (cnt2 > n / ) res.push_back(b);
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/229

类似题目:

Majority Element

Check If a Number Is Majority Element in a Sorted Array

参考资料:

https://leetcode.com/problems/majority-element-ii/

https://leetcode.com/problems/majority-element-ii/discuss/63500/JAVA-Easy-Version-To-Understand!!!!!!!!!!!!

https://leetcode.com/problems/majority-element-ii/discuss/63520/Boyer-Moore-Majority-Vote-algorithm-and-my-elaboration

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Majority Element II 求大多数之二的更多相关文章

  1. [LeetCode] Majority Element II 求众数之二

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

  2. Leetcode: 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 (众数之二)

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

  4. 229. Majority Element II求众数II

    网址:https://leetcode.com/problems/majority-element-ii/ 参考:https://blog.csdn.net/u014248127/article/de ...

  5. 229 Majority Element II 求众数 II

    给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 你的算法应该在O(1)空间中以线性时间运行. 详见:https://leetcode.com/problems/major ...

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

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

  7. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. [LeetCode] Majority Element 求大多数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

随机推荐

  1. pygame-KidsCanCode系列jumpy-part4-弹跳

    终于要到弹跳环节了,向上弹跳其实很简单,按下空格触发时,只要把y轴速度给一个向上的速度即可. Player类,新加一个jump()方法: def jump(self): self.vel.y = -2 ...

  2. Mac下不用重复输入ssh-key的密码

    重装系统,复用以前的SSH key,发现每次调用这个Key都要输入Key的密码,很繁琐,以前不是这样的哦. 更新代码.SSH服务器总是提示: Enter passphrase for .../id_r ...

  3. ABAP技术总结

      SAP ——ABAP/4 技术总结 V3.0 2014-10-14 --江正军 1. 1.1. 1.1.1. 1.2. 1.3. 1.4. 1.5. 1.6. 1.7. 1.7.1. 1.7.2. ...

  4. linux go环境安装和基本项目结构

    最近项目中要用到Go语言,所以简单总结一下安装和配置,Go这个语言本身就限定了很多规范,比如项目设置,编程风格等,开发中就不需要再因为各种规范问题纠结了,直接用官方规定的能避免很多坑,下面直接切正题, ...

  5. IndexDB 操作util

    https://dexie.org/ https://www.tangshuang.net/5668.html https://github.com/tangshuang/hello-indexedd ...

  6. 【T09】要认识到TCP是一个可靠的,但不是绝对可靠的协议

    1.稍微想一下就知道,TCP不是绝对可靠的协议,比如:网络断开,主机崩溃,无论TCP如何努力,都无法将数据传给对方. 2.考虑应用程序A向应用程序B发送数据的TCP流程,数据流从应用程序A通过他所在主 ...

  7. 不同浏览器Firefox、IE6、IE7、IE8、IE9定义不同CSS样式

    有时候我们在制作网页的时候,会遇到不同浏览器,对填充和边距显示的不同效果.导致心情纳闷现在提供解决这个困扰的方法! 对FF.Opear等支持Web标准的浏览器与比较顽固的IE浏览器进行针对性的CSS ...

  8. MinFilter(MaxFilter)快速算法C++实现

    目录 1.算法简述 1.1.MinFilter(MaxFilter) 算法简述 1.2.MinFilter(MaxFilter) 快速算法简述 2.实现代码 2.1.MinFilterOneRow 单 ...

  9. Nop--NopCommerce源码架构详解专题目录

    最近在研究外国优秀的ASP.NET mvc电子商务网站系统NopCommerce源码架构.这个系统无论是代码组织结构.思想及分层都值得我们学习.对于没有一定开发经验的人要完全搞懂这个源码还是有一定的难 ...

  10. Easyui中 messager.alert 后某文本框获得焦点

    messager.alert 后某文本框获得焦点 $.messager.alert({ title:'消息', msg:'电话号码 只能是数字!', icon: 'info', width: 300, ...