leetcode 229: Majority Element II

原创 2015年07月07日 04:40:19

Majority Element II

Total Accepted: 3172 Total Submissions: 14746

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.


[思路]

[REFERENCE] http://bookshadow.com/weblog/2015/06/29/leetcode-majority-element-ii/

观察可知,数组中至多可能会有2个出现次数超过 ⌊ n/3 ⌋ 的众数

记变量n1, n2为候选众数; c1, c2为它们对应的出现次数

遍历数组,记当前数字为num

若num与n1或n2相同,则将其对应的出现次数加1

否则,若c1或c2为0,则将其置为1,对应的候选众数置为num

否则,将c1与c2分别减1

最后,再统计一次候选众数在数组中出现的次数,若满足要求,则返回之。


[CODE]

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        // 1, 2
        List<Integer> res = new ArrayList<>();
        if(nums==null || nums.length==0) return res;
        if(nums.length==1) {
            res.add(nums[0]);
            return res;
        }
        
        int m1 = nums[0];
        int m2 = 0;
        
        int c1 = 1;
        int c2 = 0;
        
        for(int i=1; i<nums.length; i++) {
            int x = nums[i];
            if(x==m1) ++c1;
            else if(x==m2) ++c2;
            else if(c1==0) {
                m1 = x;
                c1 = 1;
            } else if(c2==0) {
                m2 = x;
                c2 = 1;
            } else {
                --c1; --c2;
            }
        }
        c1 = 0; c2 = 0;
        for(int i=0; i<nums.length; i++) {
            if(m1 == nums[i]) ++c1;
            else if(m2 == nums[i]) ++c2;
        }
        if(c1>nums.length/3) res.add(m1);
        if(c2>nums.length/3) res.add(m2);
        return res;
    }
}


相关文章推荐

Majority Element && Majority Element II

查找数组的多数元素

LeetCode 229. Majority Element II(众数II)

原题网址:https://leetcode.com/problems/majority-element-ii/ Given an integer array of size n, find all ...
  • jmspan
  • jmspan
  • 2016年04月05日 06:28
  • 694

229.Majority Element II

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

LeetCode229:Majority Element II

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

[Java] Majority Element II 找众数2

leetcode 链接 :https://leetcode.com/problems/majority-element-ii/ 题目 Given an integer array of size ...
  • lyy_hit
  • lyy_hit
  • 2015年08月13日 19:11
  • 787

leetcode229-Majority Element II(数组中出现最多的数II)

问题描述:Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The al...
  • will130
  • will130
  • 2016年04月02日 20:07
  • 276

[LeetCode 229] Majority element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algori...
  • sbitswc
  • sbitswc
  • 2015年09月02日 06:45
  • 1426

LeetCode 229. Majority Element II

做完LeetCode 169. Majority Element  后趁热打铁,做了Majority Element II。                    这道题和169相似。差别在于,要找...
  • isNoel
  • isNoel
  • 2016年09月12日 00:16
  • 170

leetcode_c++:Majority Element II (229)

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

[leetcode-229]Majority Element II(c++)

问题描述: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The ...
  • zdavb
  • zdavb
  • 2015年08月24日 20:59
  • 225
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 229: Majority Element II
举报原因:
原因补充:

(最多只允许输入30个字)