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

详见:https://leetcode.com/problems/majority-element-ii/description/

摩尔投票法 Moore Voting

Java实现:

class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> res=new ArrayList<Integer>();
if(nums==null||nums.length==0){
return res;
}
int a=nums[0],cnta=0;
int b=nums[0],cntb=0;
for (int num : nums) {
if (num == a) {
++cnta;
}else if(num == b) {
++cntb;
}else if(cnta == 0) {
a = num;
cnta=1;
}else if(cntb == 0) {
b = num;
cntb=1;
}else{
--cnta;
--cntb;
}
}
cnta=0;
cntb=0;
for(int num:nums){
if(num==a){
++cnta;
}else if(num==b){
++cntb;
}
}
if(cnta>nums.length/3){
res.add(a);
}
if(cntb>nums.length/3){
res.add(b);
}
return res;
}
}

C++实现:

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
int m = 0, n = 0, cm = 0, cn = 0;
for (auto &a : nums)
{
if (a == m)
{
++cm;
}
else if (a ==n)
{
++cn;
}
else if (cm == 0)
{
m = a, cm = 1;
}
else if (cn == 0)
{
n = a, cn = 1;
}
else
{
--cm, --cn;
}
}
cm = cn = 0;
for (auto &a : nums)
{
if (a == m)
{
++cm;
}
else if (a == n)
{
++cn;
}
}
if (cm > nums.size() / 3)
{
res.push_back(m);
}
if (cn > nums.size() / 3)
{
res.push_back(n);
}
return res;
}
};

参考:https://www.cnblogs.com/grandyang/p/4606822.html

229 Majority Element II 求众数 II的更多相关文章

  1. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  2. 229. Majority Element II求众数II

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

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

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

  4. 229. 求众数 II

    Q: 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: [3 ...

  5. Java实现 LeetCode 229 求众数 II(二)

    229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...

  6. Leetcode 229.求众数II

    求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

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

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

  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

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

随机推荐

  1. 重载OverLoad。隐藏new

    <1> using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...

  2. 【Mongodb教程 第一课 补加课】 Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接

    1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...

  3. Linux下kill命令的学习,(主要根据man手册进行的翻译)

    名字      kill -终止一个进程 格式     kill  [-s signal | -p]  [--] pid ..                                      ...

  4. Spring中AOP的使用

    问题:什么是AOP? 答:AOP基本概念:Aspect-Oriented Programming,面向方面编程的简称,Aspect是一种新的模块化机制.用来描写叙述分散在对象.类或方法中的横切关注点( ...

  5. iptraf 网卡 ip 端口 监控 netstat 关闭端口方法

    18 commands to monitor network bandwidth on Linux server – BinaryTides https://www.binarytides.com/l ...

  6. UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP

    题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...

  7. YTU 2906: 多重继承 日期与时间

    2906: 多重继承 日期与时间 时间限制: 1 Sec  内存限制: 128 MB 提交: 240  解决: 190 题目描述 在空缺的地方补全代码,只需提交补全部分即可 请用C++方式提交 #in ...

  8. ssh远程连接docker中linux(ubuntu/centos)

    ssh远程连接docker中linux(ubuntu/centos) https://www.jianshu.com/p/9e4d50ddc57e centos docker pull centos: ...

  9. 【转】axios全攻略

    随着 vuejs 作者尤雨溪发布消息,不再继续维护vue-resource,并推荐大家使用 axios 开始,axios 被越来越多的人所了解.本来想在网上找找详细攻略,突然发现,axios 的官方文 ...

  10. 小程序-demo:妹纸图

    ylbtech-小程序-demo:妹纸图 1.返回顶部 0.     1.app.js //app.js App({ onLaunch: function () { //调用API从本地缓存中获取数据 ...