LintCode Majority Number II / III
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.
Find it.
Have you met this question in a real interview? Yes
Example
Given [1, 2, 1, 2, 1, 3, 3], return 1.
Note
There is only one majority number in the array.
Challenge
O(n) time and O(1) extra space.
既然已经到1/3了不如直接用1/K的做法:
class Solution {
public:
/**
* @param nums: A list of integers
* @return: The majority number occurs more than 1/3.
*/
int majorityNumber(vector<int> nums) {
// write your code here
int stored = 2;
unordered_map<int, int> counts;
for (int e : nums) {
if (counts.size() < stored && counts.count(e) == 0) {
counts[e] = 1;
continue;
}
if (counts.count(e) == 0) {
auto iter = counts.begin();
while (iter != counts.end()) {
if (--iter->second == 0) {
iter = counts.erase(iter);
continue;
}
++iter;
}
} else {
counts[e]++;
}
}
int maxcount = -1;
int majority = 0;
for (auto& iter : counts) {
iter.second = 0;
}
for (int e : nums) {
if (counts.count(e)) {
if (++counts[e] > maxcount) {
maxcount = counts[e];
majority = e;
}
}
}
return majority;
}
};
当K=3时,由于数组中总是有数字会超过1/3个,那么我们三个一组三个一组的把(不同)数字依次除去,剩下可能又1个、2个数字的情况,当剩下数字只有一个时该数就是所求的数字,当有两个时我们是不能确定的,需要看已除去组中那个数字出现的比较多才能决定。
算法就是使用哈希来存储K-1个不同数字出现的次数,当有一个数和哈希表中任意K-1一个数都不同时,可以将这K个数作为一组整体划去(两两不同的一组数),体现为将K-1个数的计数减一(因为刚刚那个和K-1数都不同的数并没有存储到哈希表中所以不需要额外动作)。当划去过程中某个数在哈希表中计数值到达零时应该将其删去。最后再对留在哈希表中的数字做一个统计看到底那个数出现的次数多。
因而空间复杂度可以认为是O(K)的
*/
int majorityNumber(vector<int> nums, int k) {
// write your code here
int stored = k - 1;
LintCode Majority Number II / III的更多相关文章
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- Lintcode: Majority Number 解题报告
Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- [LintCode] Majority Number 求大多数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- LintCode: Single Number II
一篇解析比较详细的文章:http://www.acmerblog.com/leetcode-single-number-ii-5394.html C++ 解法(1) 求出每个比特位的数目,然后%3,如 ...
随机推荐
- 如何解决git====push 过程中出现的。error: failed to push some refs
当我们在利用git push 文件到仓库时出现了一下问题: ! [rejected] master -> master (fetch first)error: failed to push s ...
- 安卓之必须了解的实时通信(Socket)
Socket: 有服务器和客户端之分,其是对TCP/IP的封装,使用IP地址加端口,确定一个唯一的点.在Internet上的主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket ...
- 数据库占用cpu较高的查询
近来看到别人的有关数据库查询cpu占用较高的sql语句(本人sql并不好),所以查询了一下资料,记录一下,便于理解和应用. 首先,将语句贴在这里 SELECT TOP 10 --平均cpu时间 tot ...
- [原创]K8Cscan插件之存活主机扫描
[原创]K8 Cscan 大型内网渗透自定义扫描器 https://www.cnblogs.com/k8gege/p/10519321.html Cscan简介:何为自定义扫描器?其实也是插件化,但C ...
- Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器
第一步:创建 layout1.axml 来展示列表详细内容 <?xml version="1.0" encoding="utf-8"?> <L ...
- AOP切面实现原理以及多个切面切同一个地方时的优先级讲解
此博文的编写,源于前段时间的惨痛面试经历.刚好近几天尘埃落定.手头事少,遂总结一二,与各位道友分享,欢迎吐槽指正.今年年初的这段面试经历,已于之前的博文中 整理发出(https://www.cnblo ...
- Unity教程之-UGUI美术字体的制作与使用
文章转载自:http://www.unity.5helpyou.com/3211.html 游戏制作中,经常需要使用各种花哨的文字或者数字,而字体库往往不能达到我们需要的效果,因此需要一种用图片替代文 ...
- oracle12c创建用户提示ORA-65096:公用用户名或角色无效
1.背景 以前一直用的是oracle11g,创建用户一直没有问题, 今天在oracle12c上创建用户,报错了.如下图: 我很郁闷, 就打开了oracle官方网站找了下, 发现创建用户是有限制的. 2 ...
- 【JAVA集合框架一 】java集合框架官方介绍 Collections Framework Overview 集合框架总览 翻译 javase8 集合官方文档中文版
原文链接: https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html 原文内容也一并附加在本文最 ...
- shiro源码篇 - shiro的filter,你值得拥有
前言 开心一刻 已经报废了一年多的电脑,今天特么突然开机了,吓老子一跳,只见电脑管家缓缓地出来了,本次开机一共用时一年零六个月,打败了全国0%的电脑,电脑管家已经对您的电脑失去信心,然后它把自己卸载了 ...