229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
class Solution {
public:
//O(n) Space compexity
vector<int> majorityElement01(vector<int>& nums) {
vector<int> result;
unordered_map<int, int> counts;
int n = nums.size();
for(auto item : nums){
counts[item]++;
if (counts[item] > n/ ){
result.push_back(item);
counts[item] = -n; // Tricky: make sure the item only can be put into result once.
}
}
return result;
}
//We know, there could be at most two numbers can be more than 1/3
//so, same as Majority Element I problem, we can have two counters.
vector<int> majorityElement02(vector<int>& nums) {
if(nums.size()<=) return nums;
//the same algorithm as Majority Element I problem
int majority1=, majority2=, cnt1=, cnt2=;
for(auto item: nums) {
if (cnt1 == && majority2 != item ) {
majority1 = item;
cnt1 = ;
} else if (majority1 == item) {
cnt1++;
} else if (cnt2 == ) {
majority2 = item;
cnt2 = ;
} else if (majority2 == item) {
cnt2++;
} else {
cnt1--;
cnt2--;
}
}
//re-check it again, in case there has less than two numbers of majority
cnt1 = cnt2 = ;
for (auto item : nums) {
if (majority1 == item) cnt1++;
else if (majority2 == item) cnt2++;
}
vector<int> result;
if (cnt1 > nums.size()/) result.push_back(majority1);
if (cnt2 > nums.size()/) result.push_back(majority2);
return result;
}
229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数的更多相关文章
- 如何在O(n)的时间复杂度内找出数组中出现次数超过了一半的数
方法一:每次取出两个不同的数,剩下的数字中重复出现次数超过一半的数字肯定,将规模缩小化.如果每次删除两个不同的数,这里当然不是真的把它们踢出数组,而是对于候选数来说,出现次数减一,对于其他数来说,循环 ...
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
- 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...
- <C#>找出数组中重复次数最多的数值
给定一个int数组,里面存在重复的数值,如何找到重复次数最多的数值呢? 这是在某社区上有人提出的问题,我想到的解决方法是分组. 1.先对数组中的所有元素进行分组,那么,重复的数值肯定会被放到一组中: ...
- Java实现找出数组中重复次数最多的元素以及个数
/**数组中元素重复最多的数 * @param array * @author shaobn * @param array */ public static void getMethod_4(int[ ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
找出数组中的单身狗: 1. OddOccurrencesInArray Find value that occurs in odd number of elements. A non-empty ze ...
- [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
随机推荐
- 【转载】linux内核笔记之高端内存映射
原文:linux内核笔记之高端内存映射 在32位的系统上,内核使用第3GB~第4GB的线性地址空间,共1GB大小.内核将其中的前896MB与物理内存的0~896MB进行直接映射,即线性映射,将剩余的1 ...
- 协方差Covariance的表述推导
今天想了一下关于概率论的一维数据期望.方差以及高维数据的矩阵表示,突然想到为什么在一维中 方差的表示为:V(x) = E((x-E(x))2) 而到了高维,这样的表述就成了协方差呢?V(X) = E( ...
- 解决properties文件乱码问题(eclipse和MyEclipse)
windows——>Preferences——>General——>ContentTypes——>Text——>Java Properties File,设置Defaul ...
- CSS笔记(二)CSS属性选择器
对带有指定属性的HTML元素设置样式. 参考: http://www.w3school.com.cn/css/css_syntax_attribute_selector.asp 选择器 描述 [att ...
- 常用的邮箱服务器(SMTP、POP3)地址、端口
常用的邮箱服务器(SMTP.POP3)地址.端口 参考网址:http://wenku.baidu.com/link?url=IPv15rPRkd0nsuGH0Dm0A5kFyRaeHJY2_gYpDW ...
- yii 验证码的使用
在HappyController 中加入 public function actions(){ return array( // captcha action renders the CAPTCHA ...
- jQuery 中$(this).parent().parent().remove()无效。
在写文章系统的删除功能.需要删除一行数据.在删除的页面,需要jQuery 删除一hang. 局部刷新数据. $(".del").click(function(){ var id = ...
- iOS - UIScrollView
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding> @available(iOS 2.0, ...
- [转载] Google数据中心网络技术漫谈
原文: http://www.sdnlab.com/12700.html?from=timeline&isappinstalled=0#10006-weixin-1-52626-6b3bffd ...
- 子div用了float浮动之后,如何撑开父元素,让父元素div自动适应高度的问题
方法一: html: <div id="all1"> <div id="left1">1</div> <div id= ...