692. 前K个高频单词




class Solution(object):
    def topKFrequent(self, words, k):
        """
        :type words: List[str]
        :type k: int
        :rtype: List[str]
        """
        # 用字典统计每个单词的数量
        mydict = {}
        for word in words:
            if word in mydict:
                mydict[word] += 1
            else:
                mydict[word] = 1
        # 按value域降序排列,返回值是list
        mylist = sorted(mydict.items(), key=lambda x: x[1], reverse=True)
        print(mylist)
        # 设置临时容器,存放相同value值的单词
        temp = [mylist[0][0]]
        i, j = 0, 1
        ans = []
        while j < len(mylist):
            if mylist[j][1] == mylist[i][1]:
                # value值相同则将单词放入temp中,准备字母排序
                temp.append(mylist[j][0])
                # 指针后移
                i += 1
                j += 1
            else:
                # 将相同value值的按字母排序,加到ans中
                temp.sort()
                ans += temp
                # temp清空,准备记录下一轮
                temp = []
                # 指针后移
                i += 1
                j += 1
                # 开始记录新的一轮
                temp.append(mylist[i][0])
        # 最后一轮的temp还没有处理,别落下
        temp.sort()
        ans += temp
        return ans[:k]
原文地址:https://www.cnblogs.com/panweiwei/p/14025008.html