[LeetCode-JAVA] 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.

题意:找到数组中权重大于⌊ n/3 ⌋的数字(成为众数)。

思路:

为了同时满足时间复杂度和空间复杂度,不能用Map来做,根据题意,大于⌊ n/3 ⌋的数字最多不会超过两个,

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

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

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

否则,若n1或n2为空,则将其赋值为num,并将对应的计数器置为1

否则,将n1与n2中出现次数较少的数字的计数器减1,若计数器减为0,则将其赋值为num,并将对应的计数器置为1

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

代码:

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> list = new ArrayList<Integer>();
        if(nums == null || nums.length == 0)
            return list;
        int n = nums.length;
        int count = n/3;

        int num1 = nums[0];
        int num2 = nums[0];

        int count1 = 1;
        int count2 = 0;

        for(int i = 1 ; i < n ; i++){
            int temp = nums[i];
            if(temp == num1){
                count1++;
            }else if(temp == num2){
                count2++;
            }else{
                if(count2 == 0){  // 最开始
                    num2 = temp;
                    count2 = 1;
                    continue;
                }
                if(count1 < count2){
                    count1--;
                }else
                    count2--;

                if(count1 == 0){
                    num1 = temp;
                    count1 = 1;
                }
                if(count2 == 0){
                    num2 = temp;
                    count2 = 1;
                }
            }
        }
        count1 = 0;
        count2 = 0;
        for(int i = 0 ; i < n ; i++){
            if(nums[i] == num1)
                count1++;
            if(nums[i] == num2)
                count2++;
        }

        if(count1 > count)
            list.add(num1);
        if(num2 != num1 && count2 > count)
            list.add(num2);

        return list;
    }
}

参考链接:http://www.tuicool.com/articles/eA7nIzY

时间: 2024-08-06 08:52:29

[LeetCode-JAVA] Majority Element II的相关文章

[LeetCode][JavaScript]Majority Element II

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. https://leetcode.com/problems/majority-element-ii/ 使用Moore voting algorithm,时间复杂度是

[LeetCode] 229. Majority Element II 多数元素 II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2] 169. Maj

Java for LeetCode 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. 解题思路: <编程之美>寻找发帖水王的原题,两次遍历,一次遍历查找可能出现次数超过nums.length/3的数字,(出现三次不同的数即抛弃),第二次验证即可. JAVA实现如下: public Lis

LeetCode 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. 题目标签:Array 题目给了我们一个 nums array, 让我们找出所有 数量超过 n/3 的众数.这一题与 众数之一 的区别在于,众数之一 只要找到一个 众数大于 n/2 的就可以.这一题要找到所

leetcode 229. Majority Element II(多数投票算法)

就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int> majorityElement(vector<int>& nums) { int cnt1=0,cnt2=0,ans1=0,ans2=1; for(auto n:nums){ if(n==ans1){ cnt1++; } else if(n==ans2){ cnt2++; } el

(medium)LeetCode 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. 解法:参考编程之美129页,寻找发帖水王 代码如下: public class Solution { public List<Integer> majorityElement(int[] nums) {

LeetCode题解-----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. 分析: 因为要找出的是出现次数大于⌊ n/3 ⌋的元素,因此最多只可能存在两个这样的元素,而且要求O(1)的空间复杂度,因此只能使用摩尔投票法.首先我们遍历一遍数组找出两个候选元素,接着再遍历

Leetcode 229 Majority Element II

1. 问题描写叙述 在一个无序的整数数组nums[](大小为n)中.找出出现次数大于n/3的全部数.即找出数字numsi的出现次数k,满足k>?n/3?. 2. 方法与思路 首先.能够通过分析得到结论:满足条件的数字个数cnt最多为2. 证明: ifcnt>2?cnt× (?n/3?+1 )>n 超出原数组的大小. 然后,借鉴在数组中求出现次数超过一半的数这道题的思路: 1). 第一遍扫描,设两个计数器和变量记录数组nums[]中出现频率最高的数. 2). 第二遍扫描,计算着两个数出现的

【LeetCode】229. Majority Element II

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. Hint: How many majority elements could it possibly have? Do you have a better hint

Majority Element &amp;&amp; Majority Element II

Majority Element https://leetcode.com/problems/majority-element/ Difficulty: Easy 查找数组的多数元素(majority element) 多数元素为数组中出现次数多于?n/2?的元素.假设数组非空且多数元素一定存在 LeetCode的解答中给出了七种思路 第一种是Brute force solution,时间复杂度为O(n2),顾名思义,遍历数组,依次判断每个元素是否为多数元素 第二种是Hash table,时间复