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. IOS中的网络编程详解

    在移动互联网时代,几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力,如果没有了网络,也就缺少了数据变化,无论外观多 ...

  2. pair练习

    /* 编写程序读入一些列string和int型数据,将每一组存储在一个pair对象中, 然后将这些pair对象存储在vector容器里. */ #include <iostream> #i ...

  3. struts2 18拦截器详解(九)

    ScopedModelDrivenInterceptor 该拦截器处于defaultStack第八的位置,其主要功能是从指定的作用域内检索相应的model设置到Action中,该类中有三个相关的属性: ...

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

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

  5. 用Maven构建单机Mahout项目

    Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Ambari, ...

  6. uboot mmc read/write命令用法

    mmc read用来读取mmc内容到内存, mmc write用来写入内存内容到mmc中 具体用法, mmc read <device num> addr blk# cnt [partit ...

  7. CHtmlEditCtrl (2): Add a Source Text Editor to Your HTML Editor

    In a previous article, I described how to create an HTML editor using the MFC CHtmlEditCtrl class in ...

  8. 创建mysql数据库并指定编码

    xplanner的readme.txt里有句话“XPlanner has only been tested on mysql 4.x, myslq 5.0, Tomcat 5.x, java 1.4, ...

  9. logstash启动脚本

    1  nohup ./redis-server 1>log.log 2>error.log &  2 nohup ./elasticsearch -f & 3 nohup ...

  10. LCX端口内网映射转发

    这几天在渗透一家奶茶店的时候, 使用溢出攻击获得了奶茶店shell, 截屏以后发现该电脑有安装sqlserver, 但是数据库无法远程连接, 所以我决定使用端口映射工具突破端口连接限制: 我的机器地址 ...