leetcode--347:(python)Top K Frequent Elements
# 2019.7.12:
我的思路:
当知道python有一个叫Counter(计数器)就很简单了,它会统计字符串中,字符出现的次数,并返回一个元组:【0】是字符串,【1】是出现次数
我的答案:
from collections import Counter class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: counter = Counter(nums).most_common() # 按出现次数从大到小排列 alist = [] for i in range(k): alist.append(counter[i][0]) # 返回的是一个元组:【0】是字符串,【1】是出现次数 return alist