标签:pytho top k 返回 思路 self nbsp pen code ret
# 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
leetcode--347:(python)Top K Frequent Elements
标签:pytho top k 返回 思路 self nbsp pen code ret
原文地址:https://www.cnblogs.com/marvintang1001/p/11177021.html