Easy python solution


  • -2
    J
    class Solution(object):
        def majorityElement(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            tmp = {}
            res = []
            for n in list(set(nums)):
                tmp[n] = nums.count(n)
            for k,v in tmp.iteritems():
                if v > len(nums) / 3:
                    res.append(k)
            return res

  • 0
    P

    Easy solution is just a single iteration through both lists and constant space.


  • 0
    J

    Thank you for your advice, I am still learning how to make a good algorithm.


Log in to reply
 

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