leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解。
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int cnt1=,cnt2=,ans1=,ans2=;
for(auto n:nums){
if(n==ans1){
cnt1++;
}
else if(n==ans2){
cnt2++;
}
else if(cnt1==){
ans1=n;
cnt1++;
}
else if(cnt2==){
ans2=n;
cnt2++;
}
else{
cnt1--;
cnt2--;
}
}
cnt1=cnt2=;
for(auto n:nums){
if(n==ans1){
cnt1++;
}
else if(n==ans2){
cnt2++;
}
}
vector<int>ans;
if(cnt1>nums.size()/){
ans.push_back(ans1);
}
if(cnt2>nums.size()/){
ans.push_back(ans2);
}
return ans;
}
};
leetcode 229. Majority Element II(多数投票算法)的更多相关文章
- [LeetCode] 229. Majority Element II 多数元素 II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- leetcode 229 Majority Element II
这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...
- LeetCode 229. Majority Element II (众数之二)
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- LeetCode题解-----Majority Element II 摩尔投票法
题目描述: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The a ...
- Java for LeetCode 229 Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- (medium)LeetCode 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 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 【刷题-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】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
随机推荐
- swift菜鸟入门视频教程-05-控制流
本人自己录制的swift菜鸟入门.欢迎大家拍砖.有什么问题能够在这里留言. 主要内容: For 循环 While 循环 条件语句 控制转移语句(Control Transfer Statements) ...
- html5 cocos2d js Access-Control-Allow-Origin
1.No 'Access-Control-Allow-Origin' header is present on the requested 近期在接html5的渠道,遇到了跨域的问题,使用 js 的 ...
- Python中strip方法的妙用
[开胃小菜] 当提到python中strip方法,想必凡接触过python的同行都知道它主要用来切除空格.有下面两种方法来实现. 方法一:用内置函数 #<python> if __name ...
- 畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】
Problem Description 相信大家都听说一个"百岛湖"的地方吧.百岛湖的居民生活在不同的小岛中.当他们想去其它的小岛时都要通过划小船来实现.如今政府决定大力发展百岛湖 ...
- java利用爬虫技术抓取(省、市(区号\邮编)、县)数据
近期项目须要用到 城市的地址信息,但从网上下载的xml数据没有几个是最新的地址信息.....数据太老,导致有些地区不全.所以才想到天气预报官网特定有最新最全的数据.贴出代码,希望能给有相同困惑的朋友. ...
- 【Linux】 awk应用
1 统计机器上处于不同状态的所有TCP连接的个数(TCP连接是有状态连接,包含SYN_RECV, ESTABLISHED, TIME_WAIT, FIN_WAIT0, FIN_WAIT1等多种状态, ...
- python学习(八)阶段性总结
这里学习了python的一点点基础然后来总结一下 python是一个强类型的语言 数字: 数字的类型大概有:整数.浮点数.复数.固定精度的十进制数.带分子和分母的有理数 数字与字符串之间不能直接相加, ...
- Dubbo(一)Dubbo资料
这个资料绝对权威了:http://dubbo.io/user-guide/
- 16 nginx实现负载均衡
一:nginx实现负载均衡-----------------原理-------------------------- (1) 反向代理后端如果有多台服务器,自然可形成负载均衡,但proxy_pass如 ...
- android 底部菜单栏实现(转)
1.Android学习之BottomNavigationBar实现Android特色底部导航栏 2.Android底部导航栏的四种实现 3.Android BottomNavigationBar底部导 ...