Python solution sharing 56ms


  • -4
    class Solution(object):
        def majorityElement(self, nums):
            res=[]
            for i in set(nums):
                if(nums.count(i)>len(nums)/3):
                    res.append(i)
            return res
        """
        :type nums: List[int]
        :rtype: List[int]
        """

  • 0
    C
    majorElement = [ele for ele in set(nums) if nums.count(ele) > (len(nums)/3)]
    return majorElement

  • 0
    G

    The nums.count() function in your loop has time complexity O(n), so the total is not linear.


Log in to reply
 

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