【LeetCode】229. Majority Element II
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:
- How many majority elements could it possibly have?
- 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的更多相关文章
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- 【刷题-LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 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 ...
- 【LeetCode】169 - Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
随机推荐
- C++可变参数列表处理宏va_list、va_start、va_end的使用
VA_LIST是在C语言中解决变参问题的一组宏他有这么几个成员: 1)va_list型变量: #ifdef _M_ALPHA typedef struct{ char* a0; /* ...
- Redis 在线管理工具(phpRedisAdmin)介绍
phpRedisAdmin is a simple web interface to manage Redis databases. phpRedisAdmin 在 Redis clients 的列表 ...
- wifidog 源码初分析(1)-转
wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码. 在路由器上启动 wifidog 之后,wifid ...
- 编译GSLSDevil的全过程
GLSLDevil是调试OpenGL程序的工具. GLSLDevil的新版本已经改名为GLSL-Debugger, github的地址在这里:http://glsl-debugger.github ...
- win8下everything无法使用的解决方法
今日我电脑上的Everything打开后都无法使用了,只显示几个分区,重装之后暂时就好了,重启电脑又坏了 解决方法:运行services.msc,启动everything.然后重启everything ...
- 推荐一款jQuery ColorPicked 颜色拾取器插件
先看实现的效果图, 本文底部有完整demo 不想看我墨迹的可以跳过了^_^. 官网地址:http://www.eyecon.ro/colorpicker/#about 代码SVN 地址:https:/ ...
- 【转】Spring项目启动报"Could not resolve placeholder"解决方法
问题的起因: 除去properites文件路径错误.拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceho ...
- iOS 里RGB 配色 UIColor colorWithRed
//比如rgb 色值为73. 148 .230 那么ios 里面要在后面加.0f 再除以255 [bline setBackgroundColor:[UIColor colorWithRed:73.0 ...
- [Python]将Excel文件中的数据导入MySQL
Github Link 需求 现有2000+文件夹,每个文件夹下有若干excel文件,现在要将这些excel文件中的数据导入mysql. 每个excel文件的第一行是无效数据. 除了excel文件中已 ...
- .NET MVC中登陆授权过滤器的使用
1.写个类LoginAuthorityAttribute,继承自AuthorizeAttribute using System; using System.Collections.Generic; u ...