LeetCode 229. 求众数 II(Majority Element II )
题目描述
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。
说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
示例 1:
输入: [3,2,3]
输出: [3]
示例 2:
输入: [1,1,1,3,3,2,2,2]
输出: [1,2]
解题思路
由于数组中出现次数超过 $\frac{1}{3}$ 的数字最多只可能为两个,所以记录两个数字n1、n2,以及他们出现的次数c1、c2,遍历数组并做以下操作:
- 若当前两数字出现则把对应的次数加1;
- 若其中一个出现次数为0,则把当前数字赋给出现次数为0的数字,并将其出现次数置为1;
- 若当前数字不同于任何一个数字,则将两数字的出现次数都减1
最后得到两个数字以及他们出现的次数,再遍历一遍数组记录他们的出现次数,若大于 $\frac{n}{3}$ 则加入到结果中。
代码
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
if(nums.empty()) return res;
int n1 = nums[], n2 = , c1 = , c2 = ;
for(int i = ; i < nums.size(); i++){
if(nums[i] == n1) c1++;
else if(nums[i] == n2) c2++;
else if(c1 == ){
n1 = nums[i];
c1++;
}
else if(c2 == ){
n2 = nums[i];
c2++;
}
else{
c1--;
c2--;
}
}
c1 = c2 = ;
for(int i = ; i < nums.size(); i++){
if(nums[i] == n1) c1++;
else if(nums[i] == n2) c2++;
}
if(c1 > nums.size() / ) res.push_back(n1);
if(c2 > nums.size() / ) res.push_back(n2);
return res;
}
};
LeetCode 229. 求众数 II(Majority Element II )的更多相关文章
- Leetcode之分治法专题-169. 求众数(Majority Element)
Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...
- Java实现 LeetCode 229 求众数 II(二)
229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...
- Leetcode 229.求众数II
求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...
- [Swift]LeetCode229. 求众数 II | Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- [LeetCode] 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] Majority Element 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
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 ...
随机推荐
- Qt的多线程总结以及使用(一)
Qt提供QThread类以进行多任务的处理.Qt提供的线程可以做到单个进程做不到的事情.在这里实现最简单的一个多线程.最简单的线程的基类为QThread,然后需要重写QThread的run(),在ru ...
- BigDecimal与Long、int之间的相互转换
//bigDecimal 转换成 Long类型 public static Long bigDecimalToLong(BigDecimal b){ BigDecimal c = new BigDec ...
- SpringBoot+MyBatis+Mysql 6.X 版本日期型数据获,时间错乱,jason序列化时间相差8小时问题
新项目是用的springboot+mybatis+mysql 6.0.6版本的驱动包来搭建的,在使用的过程中遇到以下2个问题 从mysql取的的数据日期时间,与真实的时间往后错乱了14个小时. spr ...
- PCQQ - 发送自定义的XML卡片消息
效果: 原理: qq分享产生的xml卡片消息存储在qq内存中,可以在qq运行内存中搜索找到其xml源码,记录源码相应的内存地址,通过内存地址修改掉内存数据,再次转发这条分享的消息就会发现内容的变化. ...
- SIFT算法相关资料
SIFT算法相关资料 一.SIFT算法的教程.源码及应用软件1.ubc:DAVID LOWE---SIFT算法的创始人,两篇巨经典经典的文章http://www.cs.ubc.ca/~lowe/ 2. ...
- gitlab 错误处理
用gitolite新建项目,clone后首次push,可能会出现: $ git push No refs in common and none specified; doing nothing. Pe ...
- Ubuntu安装libssl-dev失败(依靠aptitude管理降级软件)并记录dpkg展示安装软件列表
Ubuntu 12.04LTS下直接安装 libssl-dev 失败 提示错误: $ sudo apt-get install libssl-dev Reading package lists... ...
- 《wifi加密破解论文》翻译介绍-wifi不再安全
前言 wifi的加密协议WPA2已经被破解,影响范围包括所有支持wifi的设备,包括Android,Linux,Apple,Windows,OpenBSD,联发科技,Linksys等.其中对Andro ...
- 数据结构系列文章之队列 FIFO
转载自https://mp.weixin.qq.com/s/ILgdI7JUBsiATFICyyDQ9w Osprey 鱼鹰谈单片机 3月2日 预计阅读时间: 6 分钟 这里的 FIFO 是先入先出 ...
- hivesql中的concat函数,concat_ws函数,concat_group函数之间的区别
一.CONCAT()函数CONCAT()函数用于将多个字符串连接成一个字符串.使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为 +---- ...