Leetcode: Majority Element II
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. Hint: How many majority elements could it possibly have?
Do you have a better hint? Suggest it!
参考Lintcode:Majority Number II, 那道题只需找一个,这个要找出所有,其实最多就两个
观察可知,数组中至多可能会有2个出现次数超过 ⌊ n/3 ⌋ 的众数
记变量candidate1, candidate2为候选众数; count1, count2为它们对应的出现次数
遍历数组,记当前数字为num
若num与candidate1或candidate2相同,则将其对应的出现次数加1
否则,若count1或count2为0,则将其置为1,对应的候选众数置为num
否则,将count1与count2分别减1
最后,再统计一次候选众数在数组中出现的次数,若满足要求,则返回之。
最后这个再一次统计很关键,如果众数有两个,则必然是candidate1和candidate2; 但若只有一个,candidate1 或 candidate 2, 则不一定是count多的那个,例子参1 1 1 1 2 3 2 3 4 4 4 这个 1就会被消耗过多,最后余下的反而比4少。
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<Integer>();
if (nums==null || nums.length==0) return res;
int count1=0, count2=0;
int candidate1=0, candidate2=0;
for (int i=0; i<nums.length; i++) {
if (count1 == 0) {
count1 = 1;
candidate1 = nums[i];
}
else if (count2 == 0 && candidate1 != nums[i]) {
count2 = 1;
candidate2 = nums[i];
}
else if (candidate1 == nums[i]) {
count1++;
}
else if (candidate2 == nums[i]) {
count2++;
}
else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
for (int elem : nums) {
if (elem == candidate1) count1++;
else if (elem == candidate2) count2++;
}
if (count1>0 && count1 > nums.length/3) res.add(candidate1);
if (count2>0 && count2 > nums.length/3) res.add(candidate2);
return res;
}
}
Leetcode: Majority Element II的更多相关文章
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [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 Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- 【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 ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] Majority Element 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- Android 一键直接查看Sqlite数据库数据
转自:http://www.cnblogs.com/trinea/archive/2012/11/16/2773656.html 本文主要介绍Android开发中如何一键直接查看sqlite数据库中的 ...
- 如何在html中插入视频
如何在html中插入视频 1,插入优酷视频: 在优酷分享界面有个html代码,直接复制放入body中,定义div的align居中即可 2.插入本地视频:用video属性 用mp4格式 <vid ...
- fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
给对话框添加类, 报错 CalibrateMFCDlg.h(6) : error C2504: “CDialog”: 未定义基类 等多个错误 加上 #include "afxwin.h&qu ...
- Cas服务器设置(java),java、php客户端配置
由于多个项目需要帐号的互通,所以一开始就是用cas去做的,不得不说cas要配置的东西挺多的,但是项目安全性不需要太高,所以没有做https的请求,也就是没有弄证书,这虽然省了很多时间和精力,但是项目之 ...
- NSString的常见方法
//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astrin ...
- PIVOT&UNPIVOT
如果是家电销售员,那么可能需要统计每月日销售的彩电.冰箱.空调...最大值.最小值.平均值等 如果你是耳鼻喉科医生,那么可能需要统计月度年度日接客咽炎.喉炎.鼻炎...最大值.最小值.平均值等 如果你 ...
- 过滤android应用列表(区分系统应用、第三方应用、sd卡中的应用)
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { // 系统程序 }else if ((app.flags & Applica ...
- MANIFEST.MF详解(转)
转载自http://blog.csdn.net/zhifeiyu2008/article/details/8829637 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, ...
- mac下配置和访问阿里云服务器(Ubuntu系统)
1.购买云服务器(http://www.aliyun.com/?spm=5176.3047821.1.1.vHFBuw) 注册帐号,在产品页面选择合适的服务器,进入详细页面选择配置,购买. 购买完成后 ...
- 20145211 《Java程序设计》第4周学习总结——园日涉以成趣
编程思想DRY和Once and Only Once DRY DRY原则的为"每一个知识都必须在系统内必须是单一的,明确的,权威的,具有代表性.当DRY的原则成功应用,在系统中,任何单一元素 ...