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 ...
随机推荐
- oracle学习之路(四) ---------PL/SQL 表,二维数组(TABLE)
LOB类型 ORACLE提供了LOB (Large OBject)类型.用于存储大的数据对象的类型.ORACLE眼下主要支持BFILE, BLOB, CLOB 及 NCLOB 类型. NCLOB 存储 ...
- Ruby on Rails 路由解析
为了更好的阅读体验.欢迎訪问 作者博客原文 Route是什么 Rails中URL的约定严格基于RESTful风格的.client的请求事实上是在操作一些资源.同一资源的不同的请求动作(GET, POS ...
- 蓝桥杯OJ PREV-19 九宫重排
题目描写叙述: 历届试题 九宫重排 时间限制:1.0s 内存限制:256.0MB 问题描写叙述 如以下第一个图的九宫格中,放着 1~8 的数字卡片.另一个格子空着.与空格子相 ...
- android之Context对各种服务的管理
经常,当我们须要用到服务的时候能够通果Context来获取:Context.getSystemService(name):比方:当我们想知道当前电话状态(来电/去电/sim卡状态等)时候,我们能够通过 ...
- hadoop权威指南学习
通常情况下,处理少量的大型文件更容易.更有效,为什么呢? map阶段中的键如果不需要可以忽略掉? MapReduce过程也可以用于本地文件的处理,但是如果是要使用到集群的话还需要HDFS. Data ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- ASP.NET动态网站制作(5)-- 标签语义化及知识补充
前言:这节课主要是讲标签语义化及一些知识点的补充 内容:参考老师的博文:http://www.cnblogs.com/ruanmou/p/4821894.html
- yum 安装 mysql5.5 mysql 5.6 mysql5.7
一. yum 安装mysql5.6 1. 安装仓库 要使用yum 安装mysql,需要使用mysql的yum 仓库,先从官网下载适合你的系统仓库 http://dev.mysql.com/down ...
- java项目中的classpath到底是什么
https://segmentfault.com/a/1190000015802324
- Til the Cows Come Home(最短路模板题)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description Bessie is ...