python one line solution


  • 0
    G
    from collections import Counter
    class Solution(object):
        def topKFrequent(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            """
            return map(lambda x:x[0],sorted(Counter(nums).items(),key=lambda x:x[1],reverse=True)[:k])
    

  • 0
    M

    Another one line solutoin

    import collections
    class Solution(object):
        def topKFrequent(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            """
            return map(lambda x: x[0], collections.Counter(nums).most_common(k))
    

  • 0
    W

    isn't this cheating in an interview?


  • 0
    M

    @wonderfuly
    It is tricky, but probably a extra point for Python developer interviews..


  • 0
    W

    @maxzhx I guess the interview will say: "can you implement it in another way?" haha


Log in to reply
 

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