解法: 字典统计排序 class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: cont = {} for eve in nums: if eve not in cont: cont[eve] = 1 else: cont[eve] += 1 contlist = sorted(cont.items(), key=lambda x: x[1]) res = [eve[0] for eve in contlist] return res[-k:]