Random


  • 0
    Q
    class Solution(object):
        def topKFrequent(self, words, k):
            """
            :type words: List[str]
            :type k: int
            :rtype: List[str]
            """
            word_map = {}
            for word in words:
                if word in word_map:
                    word_map[word] += 1
                else:
                    word_map[word] = 1
            keys = word_map.keys()
            keys.sort(cmp=lambda a, b: (word_map[b] - word_map[a]) if word_map[a] != word_map[b] else (-1 if a < b else 1))
            return keys[:k]
    

Log in to reply
 

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