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 ...
随机推荐
- 动态载入Layout 与 论Activity、 Window、View的关系
1)动态载入Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...
- 命令行编译sass
一.安装ruby1.需要的软件设备: 2.安装过程:点击上图“应用程序”安装即可,注意安装过程中其中三项都需要打上勾.如若没有三项都打上勾则需要修改环境变量中的path路径后添加一个分号. 3.打开c ...
- ubuntu 下开源安装
常用开源库安装: 0.安装g++: sudo apt-get install g++ 1.首先不可或缺的就是编译器与基本的函式库: sudo apt-get install build-essenti ...
- php开启pathinfo 模式
pathinfo 模式 需要 php.ini 开启下面这个参数 cgi.fix_pathinfo=1 path_info模式:http://www.xxx.com/index.php/模块/方法 ...
- 制作FAT12软盘以查看软盘的根目录条目+文件属性+文件内容
[-1]Before for specific info , please visit http://wiki.osdev.org/Loopback_Device [0]我们先上干货,看到效果后,我们 ...
- 在WPF对话框中如何验证用户提供的数据
在WPF中,MS在msdn的WPF应用程序开发中对用户输入的数据验证做了示范,基本思想就是添加各种类型的校验规则,比如最大最小值.字符串长度.是否为空等等,在后在界面绑定数据时添加数据字段的校验.这样 ...
- MongoDB安装配置(Windows)
官网下载:https://www.mongodb.com/ 百度经验:https://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html 官 ...
- 五个知识体系之-SQL学习-第二天
创建数据:INSERT INTO userinfo(userid,username,job,level1,companyage) VALUES ('001','xl001','test','P1',' ...
- TCP交换数据流——Nagle算法简单记录
Nagle算法: 该算法提出的目的是想解决网络中大量的小的TCP数据包造成网络拥塞的问题,举个例子,当客户端要发送一个字节的TCP数据包到服务器时,我们实际上产生了41字节长的分组:包括20字节的IP ...
- 九度OJ 1069:查找学生信息 (排序、查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11240 解决:3024 题目描述: 输入N个学生的信息,然后进行查询. 输入: 输入的第一行为N,即学生的个数(N<=1000) 接 ...