Majority Number III
Given an array of integers and a number k, the majority number is the number that occursmore than 1/k of the size of the array.
Find it.
Given [3,1,2,3,2,3,3,4,4,4] and k=3, return 3.
There is only one majority number in the array.
O(n) time and O(k) extra space
对于1/k 的数,每当要删除的时候,要把k的数字同时删掉,这样不会影响最终的结果。
public class Solution {
/**
* @param nums: A list of integers
* @param k: As described
* @return: The majority number
*/
public int majorityNumber(ArrayList<Integer> nums, int k) {
// write your code
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.size(); i ++){
int num = nums.get(i);
if(map.containsKey(num)){
map.put(num, map.get(num) + 1);
}else{
if(map.size() < 3){
map.put(num, 1);
}else{//decrease every count by one, remove pair which count is 0
ArrayList<Integer> removeKey = new ArrayList<Integer>();
for (Map.Entry en : map.entrySet()){
en.setValue((int)en.getValue() - 1);
if((int)en.getValue() == 0){
removeKey.add((int)en.getKey());
}
}
for(int j = 0; j < removeKey.size(); j ++){
map.remove(removeKey.get(j));
}
}
}
}
int result = 0;
int resultValue = 0;
for (Map.Entry cur : map.entrySet()){
if((int)cur.getValue() > resultValue){
result = (int)cur.getKey();
resultValue = (int)cur.getValue();
}
}
return result;
}
}
Majority Number III的更多相关文章
- 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 III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- Majority Number I & || && |||
Majority Number Given an array of integers, the majority number is the number that occurs more than ...
- 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 ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- leetcode-Single Number III 找独数
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- leetcode majority number
给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...
随机推荐
- 网络设备重的loopback接口
回环接口在我们做试验的过程有典型的应用,几乎可以离不开它,一个虚拟的接口,给我带来了很大的方便,有了回环接口,你可以不用为你的PC,来添加第二块物理网卡,就可以完成VM,服务器搭建,群集,VPN等试验 ...
- 彻底搞清楚python字符编码
在讨论python编码之前,我先了解了几种编码的由来. 一.编码类型 1.ascci码 ascci码由美国人发明,用1个字节(byte)存储英文和字符,前期用了128个,后来新加了其他欧洲国家的符号, ...
- java多线程系列(一)---多线程技能
java多线程技能 前言:本系列将从零开始讲解java多线程相关的技术,内容参考于<java多线程核心技术>与<java并发编程实战>等相关资料,希望站在巨人的肩膀上,再通过我 ...
- android 图片二维码识别和保存(二)
续上一篇,开发图片二维码识别功能后,我们对功能进行性能分析内存占用显著提高了,不使用该功能内存占用大约是147M,使用这个功能多次以后,高达203M. 因此对功能进行研究,发现每次生成的图片没有即时的 ...
- linux下的python3,virtualenv,Mysql,nginx,redis安装配置
Mysql安装和使用:点我 Redis安装和使用:点我 centos7安装Python3以及tab补全键的使用:点我 Linux下的virtualenv:点我 nginx的安装和使用:点我
- Jenkins管理插件(备份插件)
Jenkins管理插件 为了让所有的插件在 Jenkins 内可用,所有插件的列表可以访问链接 − https://wiki.jenkins-ci.org/display/JENKINS/Plugin ...
- 模块化开发之butterknife 在 library中使用
在Android开发中butterknife是一个很好的对资源初始化的工具,它可以使你的代码简洁通俗易懂,同时配合Android ButterKnife Zelezny插件可以让你写代码的速度提升至少 ...
- 关于python中的tkinter模块
python2.7和python3.6中的tkinter是两个包,不会自动升级,假如在fedora28做开发的话, 错误:用import Tkinter /import tkinter /import ...
- ThreadPoolExecutor 使用说明
它是一个ExecutorService,使用线程池中的线程执行提交的任务.通常我们使用Executors框架,定义使用. 线程池主要用来解决两类问题:通过缓存一定数量的可用线程,避免频繁的线程创建,销 ...
- Java字符串分割
java中字符串的分割函数,split("你想要分割的字符", 你想要最多分割为多少段,正整数) 注意事项: 1.分割特殊字符考虑转义字符的使用.如: . \ | 2.第二个参数: ...