python solution beats 90%


  • 0
    X
    class Solution(object):
        def topKFrequent(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            """
            l = len(nums)
            counts=dict()
            g = []
            lst = []
            for n in nums:
                counts[n] = counts.get(n,0)+1
            for key, val in counts.items():
                lst.append((val,key))
            lst.sort(reverse=True)
            l1 = len(lst)
            for i in range(k):
                g.append(lst[i][1])
            return g
    

Log in to reply
 

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