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的更多相关文章

  1. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  3. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

  4. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  5. LeetCode(169)Majority Element and Majority Element II

    一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...

  6. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  7. 【刷题-LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  8. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. [LeetCode] Majority Element 求大多数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. Translation Lookaside Buffer

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In principle, then, e ...

  2. Paging

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Both unequal fixed-si ...

  3. 汇总10.4版本ArcGIS Server与ArcMap 安装+SDE+注册数据源(非破解)

    文档参考了Server技术支持部各位前辈的总结文档. win10 + Server 10.4 +  ArcMap 10.4 + Oracle instant client (32位 和 64位) 安装 ...

  4. ExtJS 刷新或者重载Tree后,默认选中刷新前最后一次选中的节点代码片段

    //tree对象 var tree = Main.getPageControler().treePanel; //获取选中的节点 var node = tree.getSelectionModel() ...

  5. Bootstrap 辅组类和响应式工具

    1.情景文本颜色 //各种色调的字体 <p class="text-muted">Bootstrap 视频教程</p> <p class=" ...

  6. sqlserver 获取当前操作的数据库名称

    Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = ...

  7. win7下vs2010编译生成sqlite3.dll库

    http://blog.csdn.net/qing666888/article/details/53582262 http://download.csdn.net/detail/qing666888/ ...

  8. 【Android测试】【第六节】Monkey——认识和使用

    ◆版权声明:本文出自carter_dream的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4688880.html 自动化工具 接触安 ...

  9. 实验一 Java开发环境的熟悉-刘蔚然

    一.实验内容 1. 使用JDK编译.运行简单的Java程序 2. 使用Eclipse 编辑.编译.运行.调试Java程序 要求: 完成实验.撰写实验报告 统计PSP时间 二.实验过程 使用JDK编译. ...

  10. magento email模板设置相关

    magento后台 可以设置各种各样的邮件,当客户注册.下单.修改密码.邀请好友等等一系列行为时,会有相关信息邮件发出. 进入magento后台,System > Transactional E ...