java 229.多数元素二(第一).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 229.多数元素二(第一).java相关的知识,希望对你有一定的参考价值。

public class Solution {
    public boolean isValid(int[] nums, int len, int candidate) {
        int res = 0;
        for (int num : nums) {
            if (num == candidate) res++;
        }
        return res > len/3;
    }
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        if (nums == null || nums.length < 1) return res;
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int len = nums.length;
        for (int num : nums) {
            if (map.size() < 3 || map.containsKey(num)) {
                map.put(num, map.getOrDefault(num,0) + 1);
            } else if (!map.containsKey(num)) {
                Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry<Integer, Integer> entry = entries.next();
                    if(entry.getValue() == 0) {
                        entries.remove();
                    } else {
                        int key = entry.getKey();
                        map.put(key,map.get(key) - 1);
                    }
                    //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                }
                //for (Map.Entry<Integer, Integer> entry : map.entrySet()) { // java.util.ConcurrentModificationException
                    //if(entry.getValue() == 0) {
                        //map.remove(entry.getKey());
                    //} else {
                        //int key = entry.getKey();
                        //map.put(key,map.get(key) - 1);
                    //}
                //}
            }
        }
        for(Integer key: map.keySet()) {
            if (isValid(nums, len, key)) {
                res.add(key);
            }
        }
        return res;
    }
}
public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> arrayList = new ArrayList<Integer>();
        
        if(nums == null || nums.length == 0) {
            return arrayList;
        }
       
        int count1 = 0, count2 = 0, maj1 = nums[0], maj2 = nums[0];
        
        for(int i = 0; i < nums.length; i++) {
            if(nums[i] == maj1) {
                count1++;
            }
            else if(nums[i] == maj2) {
                count2++;
            }
            else if(count1 == 0) {
                maj1 = nums[i];
                count1++;
            }
            else if(count2 == 0) {
                maj2 = nums[i];
                count2++;
            }
            else {
                count1--;
                count2--;
            }
        }
        
        count1 = 0;
        count2 = 0;
        
        for(int i = 0; i < nums.length; i++) {
            if(nums[i] == maj1) {
                count1++;
            }
            else if(nums[i] == maj2) {
             count2++;   
            }
        }
        
        if(count1 > nums.length/3) {
            arrayList.add(maj1);
        }
        
        if(count2 > nums.length/3) {
            arrayList.add(maj2);
        }
        
        return arrayList;
    }
}

以上是关于java 229.多数元素二(第一).java的主要内容,如果未能解决你的问题,请参考以下文章

java 229.多数元素二(第一).java

java 229.多数元素二(第一).java

java 229.多数元素二(第一).java

java 229.多数元素二(第一).java

题目地址(229. 多数元素 II)

题目地址(229. 多数元素 II)