Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Hint:

  1. How many majority elements could it possibly have?
  2. Do you have a better hint? Suggest it!

超过⌊ n/k ⌋ 最多有(k-1)个结果。k=3时最多2个结果。

因此设置两个candidate进行判断。

注意:留到最后的candidate不代表真正的结果。举例[3,2,3],2是candidate但不是结果。

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> ret;
if(nums.empty())
return ret;
int candidate1 = nums[];
int count1 = ;
int candidate2 = nums[];
int count2 = ;
for(int i = ; i < nums.size(); i ++)
{
if(count1 != && count2 != )
{
if(nums[i] == candidate1)
count1 ++;
else if(nums[i] == candidate2)
count2 ++;
else
{
count1 --;
count2 --;
}
}
else if(count1 != && count2 == )
{
if(nums[i] == candidate1)
count1 ++;
else
{
candidate2 = nums[i];
count2 = ;
}
}
else if(count1 == && count2 != )
{
if(nums[i] == candidate2)
count2 ++;
else
{
candidate1 = nums[i];
count1 = ;
}
}
else
{
candidate1 = nums[i];
count1 = ;
}
}
if(count1 != )
{
count1 = ;
for(int i = ; i < nums.size(); i ++)
{
if(nums[i] == candidate1)
count1 ++;
}
if(count1 > nums.size()/)
ret.push_back(candidate1);
}
if(count2 != )
{
count2 = ;
for(int i = ; i < nums.size(); i ++)
{
if(nums[i] == candidate2)
count2 ++;
}
if(count2 > nums.size()/)
ret.push_back(candidate2);
}
return ret;
}
};

【LeetCode】229. Majority Element II的更多相关文章

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

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

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

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

  3. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

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

  4. LeetCode OJ 229. Majority Element II

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

  5. 【LeetCode】169 - Majority Element

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

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

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

  7. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  8. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  9. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

随机推荐

  1. git pull fails “unable to resolve reference” “unable to update local ref”

    问题 由于有人rebase了分支,或者不知道怎么搞的.其他人拉取代码的时候,发现拉不下来. >git fetch error: cannot lock ref 'refs/remotes/ori ...

  2. 教程 | Kaggle网站流量预测任务第一名解决方案:从模型到代码详解时序预测

    https://mp.weixin.qq.com/s/JwRXBNmXBaQM2GK6BDRqMw 选自GitHub 作者:Artur Suilin 机器之心编译 参与:蒋思源.路雪.黄小天 近日,A ...

  3. Eclipse Maven项目报错1之JAVA编译版本报错

    一.错误Dynamic Web Module 3.0 requires Java 1.6 or newer 解决办法,在pom.xml文件中增加JAVA版本的属性配置,如下 <!-- add b ...

  4. idea缓存目录mac cache

    IDEA如果出现卡顿,Index疯狂扫描,建议清空一下如下目录 ~/Library/Caches/IntelliJIdea2017.3 Resource nexus-maven-repository- ...

  5. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  6. js获取当月最后一天

    构造函数 new Date(); new Date(value); new Date(dateString); new Date(year, month[, day[, hour[, minutes[ ...

  7. Disqus评论框改造工程-Jekyll等静态博客实现Disqus代理访问

    文章最初发表于szhshp的第三边境研究所转载请注明 关于博客评论 六月多说挂了,地球人都知道. 倡言.云跟帖.来必力都很烂,地球人都知道. 转Disqus的都是人才. Disqus使用中遇到的问题 ...

  8. mysql存储过程,游标实例

    CREATE DEFINER=`root`@`%` PROCEDURE `vir`.`task_payment_byonlinedown`()begin declare _mobile varchar ...

  9. keytab生成不了

    vim /var/kerberos/krb5kdc/kadm5.acl 将*e改成* /etc/init.d/kadmin restart 重启kadmin

  10. JPA的配置文件

    一.引入包 <dependencies> <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistenc ...