leetcode--347:(python)Top K Frequent Elements
2020-12-13 06:23
标签:pytho top k 返回 思路 self nbsp pen code ret # 2019.7.12: 当知道python有一个叫Counter(计数器)就很简单了,它会统计字符串中,字符出现的次数,并返回一个元组:【0】是字符串,【1】是出现次数 leetcode--347:(python)Top K Frequent Elements 标签:pytho top k 返回 思路 self nbsp pen code ret 原文地址:https://www.cnblogs.com/marvintang1001/p/11177021.html我的思路:
我的答案:
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
文章标题:leetcode--347:(python)Top K Frequent Elements
文章链接:http://soscw.com/essay/32923.html