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. nginx location 匹配顺序

    location 匹配的原型是这样的:location [=|~|~*|^~|@] /uri/ { … } “=”是精确匹配“@”是命名的location ,在正常的location 匹配中不会使用, ...

  2. php-->mongodb[curd操作]

    <?php /** * PHP操作MongoDB学习笔记 */ //************************* //**   连接MongoDB数据库  **// //********* ...

  3. 不遗留问题-menu数据拼装

    DROP TABLE IF EXISTS `menu0910`; CREATE TABLE `menu0910` ( `id` ) NOT NULL AUTO_INCREMENT, `menu` ) ...

  4. transform animation transition css3动画

    transform 定义   transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 值 应用  如果transform与transition联合起 ...

  5. BLE协议栈及传统蓝牙协议栈对比图

    1. BLE协议栈的层次图如下: 主机控制接口层: 为主机和控制器之间提供标准通信接口 逻辑链路控制及自适应协议层: 为上层提供数据封装服务 安全管理层: 定义配对和密钥分配方式,为协议栈其他层与另一 ...

  6. HTTP访问的两种方式(HttpClient+HttpURLConnection)整合汇总对比

    HttpClient: HttpClient是Apache Jakarta Common下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版 ...

  7. 谈谈.NET中常见的内存泄露问题——GC、委托事件和弱引用

    其实吧,内存泄露一直是个令人头疼的问题,在带有GC的语言中这个情况得到了很大的好转,但是仍然可能会有问题.一.什么是内存泄露(memory leak)?内存泄露不是指内存坏了,也不是指内存没插稳漏出来 ...

  8. Java学习-022-Properties 文件数据写入

    Properties 配置文件写入主要通过 Properties.setProperty 和 Properties.store 两个方法,此文以一个简单的 properties 文件写入源码做示例. ...

  9. NavigationController popToViewController跳转之前任意ViewController方法

    NSArray *viewControllers = self.navigationController.viewControllers;A *viewController = [viewContro ...

  10. Thinkpad E430 Ubuntu 14.04 无线网卡驱动

    为了重新安装正确的无线网卡的驱动,所以要先弄清楚我的笔记本上的无线网卡的具体的型号.打开终端,用如下命令/方法查看:lspci,找到输出信息中,有关无线网卡的部分.发现型号是:BCM43142 先用有 ...