zoukankan      html  css  js  c++  java
  • 229. 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.

    链接: http://leetcode.com/problems/majority-element-ii/

    题解:

    依然是Boyer-Morrer Voting Algorithm。这回我们要设置两个变量来进行voting。

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public List<Integer> majorityElement(int[] nums) {
            List<Integer> res = new ArrayList<>();
            if(nums == null || nums.length == 0)
                return res;
            int candidate1 = 0, candidate2 = 0, count1 = 0, count2 = 0;
            
            for(int i : nums) {
                if(i == candidate1)
                    count1++;
                else if(i == candidate2)
                    count2++;
                else if(count1 == 0) {
                    candidate1 = i;
                    count1 = 1;
                } else if (count2 == 0) {
                    candidate2 = i;
                    count2 = 1;
                } else {
                    count1--;
                    count2--;
                }
            }
            
            count1 = 0;
            count2 = 0;
            
            for(int i : nums) {
                if(i == candidate1)
                    count1++;
                else if(i == candidate2)
                    count2++;
            }
            
            if(count1 > nums.length / 3)
                res.add(candidate1);
            if(count2 > nums.length /3)
                res.add(candidate2);
            
            return res;
        }
    }

    二刷:

    方法同一刷一样。一定要注意遍历数组的时候先判断num是否和num1和num2相等,再考虑count1 == 0 和count2 == 0的情况。最后便利完毕以后还要再加一个验证步骤。

    Java:

    public class Solution {
        public List<Integer> majorityElement(int[] nums) {
            List<Integer> res = new ArrayList<>();
            if (nums == null) return res;
            int num1 = 0, num2 = 0, count1 = 0, count2 = 0;
            
            for (int num : nums) {
                if (num == num1) {
                    count1++;
                } else if (num == num2) {
                    count2++;
                } else if (count1 == 0) {
                    num1 = num;
                    count1++;
                } else if (count2 == 0) {
                    num2 = num;
                    count2++;
                } else {
                    count1--;
                    count2--;
                }
            }
            
            count1 = 0;
            count2 = 0;
            for (int num : nums) {
                if (num == num1) count1++;
                else if (num == num2) count2++;
            }
            if (count1 > nums.length / 3) res.add(num1);
            if (count2 > nums.length / 3) res.add(num2);
            return res;
        }
    }

    三刷:

    跟二刷一样。要注意先判断的是num和candidates的关系,其次再判断count

    Java:

    public class Solution {
        public List<Integer> majorityElement(int[] nums) {
            List<Integer> res = new ArrayList<>();
            if (nums == null || nums.length == 0) return res;
            int candidate1 = 0, candidate2 = 0, count1 = 0, count2 = 0;
            for (int num : nums) {
                if (num == candidate1) {
                    count1++;
                } else if (num == candidate2) {
                    count2++;
                } else if (count1 == 0) {
                    candidate1 = num;
                    count1++;
                } else if (count2 == 0) {
                    candidate2 = num;
                    count2++;
                } else {
                    count1--;
                    count2--;
                }
            }
            count1 = 0;
            count2 = 0;
            for (int num : nums) {
                if (num == candidate1) count1++;
                else if (num == candidate2) count2++;
            }
            if (count1 > nums.length / 3) res.add(candidate1);
            if (count2 > nums.length / 3) res.add(candidate2);
            return res;
        }
    }

    Reference:

    https://leetcode.com/discuss/42768/o-n-time-o-1-space

    https://leetcode.com/discuss/49596/clear-o-n-solution-in-python-no-data-structure-or-sort

    https://leetcode.com/discuss/46560/boyer-moore-method-java-implementation

    https://leetcode.com/discuss/56737/my-o-n-time-solution-20ms

    https://leetcode.com/discuss/43580/my-c-solution

    https://leetcode.com/discuss/48238/c%23-solution-with-o-n-time-and-o-1-space-via-improved-quick-sort

    https://leetcode.com/discuss/42806/boyer-moore-majority-vote-algorithm-generalization

    https://leetcode.com/discuss/43248/boyer-moore-majority-vote-algorithm-and-my-elaboration

  • 相关阅读:
    JAVA流操作
    Ubuntu 14.04 忘记root密码
    产出与产能要平衡-《高效能人士的七个习惯》随笔三
    重新认识自我-《高效能人士的七个习惯》随笔二
    高效能人士的七个习惯 随笔一
    松下A6伺服器通信设定
    未能加载文件或程序集 或它的某一个依赖项。试图加载格式不正确的程序。问题解决
    keil中出现Undefined symbol FLASH_PrefetchBufferCmd (referred from main.o)等问题解决办法
    c语言中static的用法,包括全局变量和局部变量用static修饰
    STM32启动文件详细解析(V3.5.0) 以:startup_stm32f10x_hd.s为例
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4997354.html
Copyright © 2011-2022 走看看