Majority Number III
Question:
Given an array of integers and a number k, the majority number is the number that occurs more 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
.
1 public class Solution { 2 /** 3 * @param nums: A list of integers 4 * @param k: As described 5 * @return: The majority number 6 */ 7 public int majorityNumber(ArrayList<Integer> nums, int k) { 8 if(nums == null || nums.size() == 0) { 9 return Integer.MIN_VALUE; 10 } 11 12 //most k - 1 values in the map 13 HashMap<Integer, Integer> counters = new HashMap<Integer, Integer>(); 14 for(int num : nums) { 15 if(counters.containsKey(num)) { 16 counters.put(num, counters.get(num) + 1); 17 }else { 18 if(counters.size() < k - 1) { 19 counters.put(num, 1); 20 }else { 21 update(counters); 22 } 23 } 24 } 25 26 //corner case 27 if(counters.size() == 0) { 28 return Integer.MIN_VALUE; 29 } 30 31 //clear count 32 for(int key : counters.keySet()) { 33 counters.put(key, 0); 34 } 35 36 //recalculate the counter 37 for(int num : nums) { 38 if(counters.containsKey(num)) { 39 counters.put(num, counters.get(num) + 1); 40 } 41 } 42 43 int result = 0, maxCount = Integer.MIN_VALUE; 44 for(int key : counters.keySet()) { 45 int count = counters.get(key); 46 if(count > maxCount) { 47 result = key; 48 maxCount = count; 49 } 50 } 51 52 return result; 53 } 54 55 private void update(HashMap<Integer, Integer> counters) { 56 ArrayList<Integer> removeList = new ArrayList<Integer>(); 57 for(int key : counters.keySet()) { 58 counters.put(key, counters.get(key) - 1); 59 if(counters.get(key) == 0) { 60 removeList.add(key); 61 } 62 } 63 64 for(int key : removeList) { 65 counters.remove(key); 66 } 67 } 68 }
转载于:https://www.cnblogs.com/billzhou0223/p/5150145.html
Majority Number III相关推荐
- 【Lintcode】046.Majority Number
题目: Given an array of integers, the majority number is the number that occurs more than half of the ...
- leetcode majority number
给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...
- leetcode:Majority Number
1.Given an array of integers, the majority number is the number that occursmore than half of the siz ...
- 【?异或】LeetCode 260. Single Number III
LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...
- leetcode 260. Single Number III | 260. 只出现一次的数字 III(位运算:分组异或)
题目 https://leetcode.com/problems/single-number-iii/ 题解:分组异或 参考1:讨论区题解 you know you can eliminate dou ...
- leetcode 算法解析(一):260. Single Number III(C++版本和自己的注解)
这个题来自<剑指offer>但是书上上感觉讲解不太详细,还是看博客吧(我把下面博客改写成了C++版本运行通过) 注意这个题的相关代码中,输入的数组只能有两个数出现一次,如果有第三个数出现一 ...
- lintcode 中等题:Single number III 落单的数III
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- LeetCode Single Number III(位操作)
问题:给出一个数组,有两个数只出现一次,其它都出现两次. 思路:先对数组求异或,得到两个数异域的结果.然后确定差异的最低位.再将数组与这个差异位作异域,得到其中的一个数.再与异域结果异域后即得到另外一 ...
- 【LeetCode】-- 260. Single Number III
问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...
- 260. Single Number III
题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
最新文章
- 任正非签发最新电邮:过去我们是为了赚点小钱,现在是要战胜美国
- UA PHYS515A 电磁理论IV 时变电磁场理论6 用含时Green函数求解时变电磁场问题的例子
- Shiro框架:缓存、session会话、自定义FormAuthenticationFilter、RemenberMe
- 如何检查数字是否为2的幂
- Python模块的使用
- IE6下实现Width:auto
- 【预测模型】自回归(AR)模型
- 计算机网络之TCP报文
- WPF 一个简单的颜色选择器
- 检验检测系统管理服务器,检验检测管理平台
- Coursera Big Data系列课程笔记1
- B站笔试真题之[编程题]扭蛋机
- win10千万不要重置_Win10系统如何自动恢复到出厂设置,小白式操作教程
- gensim训练wiki中文词向量
- 糜烂性胃炎吃什么药?
- 性能服务器漫画免费下拉式,奇妙漫画免费漫画
- 屏幕挂灯的智商税- 小米真不香
- 用Python写一个新年倒计时
- linux 游戏下载论坛,LINUX下的各种游戏
- 一步步教你怎么用Python写贪吃蛇游戏
热门文章
- DevOps使用教程 华为云(19)git diff查看刚刚更新的文件的差异
- 第一类丢失更新和第二类丢失更新是什么 区别
- ios低版本、微信浏览器低版本 不能使用的js语法总结
- python输出特征相关矩阵_两个特征矩阵的有效成对相关
- Php的入栈,PHP实现的栈数据结构示例【入栈、出栈、遍历栈】
- centos 编译Qt5 mysql驱动_centos7安装编译mysql的驱动的问题
- java基础总结03-进制
- LocalDateTime时间加减法
- C语言w10输入法打不出中文,win10系统输不了中文怎么办
- npm安装vue-cli时速度慢,fetchMetadata经常卡住并报异常