LeetCode 229. Majority Element II(众数II)

原题网址:https://leetcode.com/problems/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.

方法一:先排序,然后统计。

public class Solution {
    public List majorityElement(int[] nums) {
        List results = new ArrayList<>();
        if (nums == null || nums.length == 0) return results;
        Arrays.sort(nums);
        int prev = nums[0];
        int count = 1;
        for(int i=1; i<=nums.length; i++) {
            if (i nums.length/3) results.add(prev);
                    prev = nums[i];
                    count = 1;
                }
            } else {
                if (count > nums.length/3) results.add(prev);
            }
        }
        return results;
    }
}

方法二:摩尔投票算法(Moore Vote Algorithm)

public class Solution {
    public List majorityElement(int[] nums) {
        int num1 = 0, num2 = 1;
        int count1 = 0, count2 = 0;
        for(int num: nums) {
            if (count1 == 0) {
                num1 = num;
                count1 = 1;
            } else if (num1 == num) {
                count1 ++;
            } else if (count2 == 0) {
                num2 = num;
                count2 = 1;
            } else if (num2 == num) {
                count2 ++;
            } else {
                count1 --;
                count2 --;
                if (count1 == 0 && count2 > 0) {
                    num1 = num2;
                    count1 = count2;
                    num2 = 0;
                    count2 = 0;
                }
            }
        }
        if (count1 > 0) {
            count1 = 0;
            for(int num: nums) if (num1 == num) count1 ++;
        }
        if (count2 > 0) {
            count2 = 0;
            for(int num: nums) if (num2 == num) count2 ++;
        }
        List results = new ArrayList<>();
        if (count1*3>nums.length) results.add(num1);
        if (count2*3>nums.length) results.add(num2);
        return results;
    }
}
LeetCode 229. Majority Element II(众数II)_第1张图片

感谢网友,参考文章:

http://www.programcreek.com/2014/07/leetcode-majority-element-ii-java/

http://www.cnblogs.com/grandyang/p/4606822.html

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

http://blog.csdn.net/xudli/article/details/46784149

http://codechen.blogspot.com/2015/06/leetcode-majority-element-ii.html

http://www.cs.utexas.edu/~moore/best-ideas/mjrty/

https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm


方法三:保存三个最高频数字作为候选,该方法与摩尔算法等效。

public class Solution {
    public List majorityElement(int[] nums) {
        int[] counts = new int[3];
        int[] recents = new int[3];
        int size = 0;
        for(int i=0; i 0 && recents[0] == nums[i]) counts[0] ++;
            else if (size > 1 && recents[1] == nums[i]) counts[1] ++;
            else if (size > 2 && recents[2] == nums[i]) counts[2] ++;
            else if (size < 3) {
                recents[size] = nums[i];
                counts[size] = 1;
                size ++;
            } else {
                int min = 0;
                if (counts[1] < counts[min]) min = 1;
                if (counts[2] < counts[min]) min = 2;
                recents[min] = nums[i];
                counts[min] = 1;
            }
        }
        List results = new ArrayList<>();
        for(int i=0; inums.length/3) results.add(recents[i]);
        }
        return results;
    }
}


你可能感兴趣的:(排序,众数,频率,摩尔投票,最高频,困难,第K个,前K个)