[leedcode 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.

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        /*首先,可以通过分析得到结论:满足条件的数字个数cnt最多为2。
  证明: ifcnt>2⇒cnt× (⌊n/3⌋+1 )>n 超出原数组的大小。
  然后,借鉴在数组中求出现次数超过一半的数这道题的思路:
  
  1). 第一遍扫描,设两个计数器和变量记录数组nums[]中出现频率最高的数。
  2). 第二遍扫描,计算着两个数出现的次数。
  3). 判断这两个数是否符合要求,符合则存入结果集。
  需要注意的是:
  第一个for循环的判断顺序,首先要判断是否等于m或n
  */
        List<Integer> res=new ArrayList<Integer>();
        if(nums==null||nums.length<=0) return res;
        int m=0;
        int n=0;
        int countm=0;
        int countn=0;

        for(int i=0;i<nums.length;i++){

           /*if(m==nums[i]) countm++;
           else if(n==nums[i]) countn++;
           else if(countm==0){
               countm=1;
               m=nums[i];
           }else if(countn==0){
               countn=1;n=nums[i];
           }else {
                countm--;
                countn--;
            }*/

            if(m==nums[i]||countm==0){
                countm++; m=nums[i];
            }else if(n==nums[i]||countn==0){
                countn++; n=nums[i];
            }else{
                countm--;countn--;
            }
        }
        countm=0;
        countn=0;
       for(int i=0;i<nums.length;i++){
           if(nums[i]==m) countm++;
           else if(nums[i]==n) countn++;
       }
       if(countm>nums.length/3) res.add(m);
       if(countn>nums.length/3) res.add(n);
        return res;
    }
}
时间: 2024-08-30 04:23:27

[leedcode 229] Majority Element II的相关文章

169. Majority Element &amp;&amp; 229. Majority Element II

169. Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Hide T

【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

LeetCode OJ 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. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! [题目分析]

[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

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

229. Majority Element II java solutions

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! Subscr

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. 用的是摩尔投票法,原版的算法是找一个数组出现次数大于数组长度半数的数(只有当数组中存在这样的数时,这个算法才会生效,而且不难理解,如果有也只可能有一个),方法就是定义一个变量m,记录当前的候选数字,再定义

(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 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). 第二遍扫描,计算着两个数出现的