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之网络编程NSURLConnection

    通过NSURLConnection主要通过四个类进行网络访问:NSURL,NSURLRequest,NSMutableURLRequest,NSURLConnection 一.基本知识 (1)NSUR ...

  2. SSE,MSE,RMSE,R-square指标讲解

    SSE(和方差.误差平方和):The sum of squares due to errorMSE(均方差.方差):Mean squared errorRMSE(均方根.标准差):Root mean ...

  3. StackPanel

    StackPanel 的 HorizontalAlignment 属性和 VerticalAlignment 属性 默认情况下,这两个属性都被设置为 Stretch.

  4. Axure-Axure RP For Chrome 演示扩展

    Axure RP生成的Html原型,其中包含JS文件,在本地进行演示时浏览器IE会弹出安全提醒.谷歌浏览器Chrome则需要在线安装一个Axure的扩展工具才可以演示. Axure RP Extens ...

  5. JAVA-SpringMVC开发第一个应用

    找到eclipse工具路径 打开eclipse.exe 选择workspace的存放位置,点击ok 点击file-new 选择web-dynamic web project(动态web项目)-next ...

  6. Centos配置为驱动程序开发环境

    安装完centos后,写了一个驱动测试程序Hello.编译过程出现如下错误: make: *** /lib/modules/2.6.32-220.4.1.el6.i686/build: No such ...

  7. vim高级用法

    http://vim.wikia.com/wiki/Using_command-line_history ----------------------------------------------- ...

  8. Structured Streaming编程向导

    简介 Structured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark ...

  9. Node js : Best way to define entity class

      If you start to use a DB like mongo, you might be better off creating objects with mongoose but th ...

  10. XE6入门(一)Hello World

    XE6的IDE已经设计的非常棒了,是该放弃D7了,投入XE6的怀抱.. 本人用的XE6版本是 Embarcadero.Delphi.XE6.RTM.Inc.Update1.v20.0.16277.12 ...