R4-排序篇
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
#使用哈希表记录值,再sort
dict=defaultdict(int)
ret=[]
for num in nums:
dict[num]+=1
the=sorted(dict.keys(),key=lambda x:dict[x], reverse=True)
for i in range(k):
ret.append(the[i])
return ret