【刷题-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.
Note: The algorithm should run in linear time and in O(1) space.
Example 1:
Input: [3,2,3]
Output: [3]
Example 2:
Input: [1,1,1,3,3,2,2,2]
Output: [1,2]
解 摩尔投票法
回顾求解超过一半的数字。假设超过一半的数字是x,剩下数字的集合记做是Y(\(|Y|<\frac{n}{2}\)),对于任意的\(y_i\in Y\),都能有一个x和其配对,最后还会剩下一个x或者刚好配对完。具体实现为:
实现的算法从第一个数开始扫描整个数组,有两个变量(参考第一答题者的变量名)major和count。其实这两个变量想表达的是一个“隐形的数组”array,array存储的是“当前暂时无法删除的数字”,我们先不要管major和count,只考虑这个array,同时再维护一个结果数组result,result里面存储的是每次删除一对元素之后的当前结果。[]
实现的算法从第一个数开始扫描整个数组,有两个变量(参考第一答题者的变量名)major和count。其实这两个变量想表达的是一个“隐形的数组”array,array存储的是“当前暂时无法删除的数字”,我们先不要管major和count,只考虑这个array,同时再维护一个结果数组result,result里面存储的是每次删除一对元素之后的当前结果。[https://www.zhihu.com/question/49973163/answer/235921864]
对于超过1/3的数字,必然最多有2两个。思路与求1/2的类似
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int m, n, cnt_m = 0, cnt_n = 0;
for(int x : nums){
if(m == x){
cnt_m++;
}else if(n == x){
cnt_n++;
}else if(cnt_m == 0){
cnt_m = 1;
m = x;
}else if(cnt_n == 0){
cnt_n = 1;
n = x;
}else{
cnt_m--;
cnt_n--;
}
}
cnt_m = 0, cnt_n = 0;
for(int x: nums){
if(x == m)cnt_m++;
else if(x == n)cnt_n++;
}
vector<int>res;
if(cnt_m > nums.size() / 3)res.push_back(m);
if(cnt_n > nums.size() / 3)res.push_back(n);
return res;
}
};
【刷题-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 ...
- 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 ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...
- (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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
随机推荐
- CF977C Less or Equal 题解
Content 给定一个 \(n\) 个数的数列 \(a_1,a_2,a_3,...,a_n\) 和一个数 \(k\),试找出这样的一个数 \(x\),使得数列中有 \(k\) 个数小于等于 \(x\ ...
- CF31A Worms Evolution 题解
Content 有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\),试找出一个三元组 \((i,j,k)\),使得 \(a_i=a_j+a_k\). 数据范围:\(3\l ...
- LuoguP7127 「RdOI R1」一次函数(function) 题解
Content 设 \(S_k\) 为直线 \(f(x)=kx+k-1\),直线 \(f(x)=(k+1)x+k\) 与 \(x\) 轴围成的三角形的面积.现在给出 \(t\) 组询问,每组询问给定一 ...
- shiro验证时,当authenticationStrategy为AllSuccessfulStrategy时
shiro验证时,当authenticationStrategy为AllSuccessfulStrategy时,如果某一个验证出错,那么 PrincipalCollection principalCo ...
- 复杂SQL案例:用户退款信息查询
供参考: select t3.course_id 课程id, t3.user_id 用户ID, u.user_full_name 姓名, -- u.phone, concat(u.company,' ...
- 7、滑动窗口套路算法框架——Go语言版
前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...
- ubuntu 16.04 server安装Bittorrent Transmission
访问web服务 使用http://192.168.1.8:9091 这样的方式管理下载. http://192.168.1.8:9091/transmission/web/ 操作服务 sudo ser ...
- 【linux项目】lichee nano linux烧写
目录 前言 参考: 安装交叉编译链 搭建 SPI FLASH 烧录环境 让芯片进入烧写模式 sunxi 烧写命令 u-boot 裁剪 拉取 u-boot 源码 配置 u-boot 检查 flash 驱 ...
- 【LeetCode】51. N-Queens 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...