Java passed Solution


  • -9
    S
      Set<Integer> result = new TreeSet<Integer>();
        Hashtable<Integer, Integer> numbers = new Hashtable<Integer, Integer>();
        int n = nums.length;
        if(n <=0)
            return  Collections.emptyList();
        int m= n/3;
    
        for (int i = 0; i < n; i++) {
            Integer r = numbers.get(nums[i]);
            if( r==null)
                r=0;
            if ((r.intValue()+1) >m) {
                result.add(nums[i]);
            } else
                numbers.put(nums[i], r.intValue()+1);
        }
    
        return new ArrayList(result);
    

    The main idea behind this algorithm is: 1 use map to store the count 2.use set to eliminate duplication elements.


  • 2
    N

    Your solution is neat and works well. However, the question asks us to use constant space i.e. O(1) space. Using Map / Set, linear space is used which is not allowed.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.