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 ...
随机推荐
- code::blocks(版本号10.05) 配置opencv2.4.3
(1)首先下载opencv2.4.3, 解压缩到D:下: (2)配置code::blocks, 详细操作例如以下: 第一步, 配置compiler, 操作步骤为Settings -> Comp ...
- nodejs 简单的备份github代码初版
传送门:http://www.jianshu.com/p/002efed0d3af 我的代码: const https = require('https'); const fs = require(& ...
- apache common包 CollectionUtils 使用 详解
集合判断: 例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...
- caffe2--------ImportError: No module named past.builtins
whale@sea:~/anaconda2/lib/python2.7/site-packages$ python Python 2.7.14 |Anaconda custom (64-bit)| ( ...
- Centos 7.0系统服务管理
从Centos7开始,不再用sysvinit管理系统服务了,而是改用了systemd,因此对系统服务管理方法已经变更,以下简述 1.查看当前所有系统服务的状态 systemctl 2.查看指定系统服务 ...
- Erlang 在erlang项目中使用protobuf
protobuf是google的一个序列化框架,类似XML,JSON,其特点是基于二进制,比XML表示同样一段内容要短小得多,还可以定义一些可选字段,广泛用于服务端与客户端通信.文章将着重介绍在erl ...
- IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(三)
IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(三) 后台服务用户与认证 新建一个空的.net core web项目Demo.Chat,端口配置为 ...
- C# xml读取操作
以下xml: <Project> <ProjectMains> <ProjectMain Action="added"> <Project ...
- CentOS系统环境下安装MongoDB
(1)进入MongoDB下载中心:http://www.mongodb.org/downloads We recommend using these binary distributions (官方推 ...
- python跳坑手记
解决python同目录报错:import util 改成 from . import util