class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
a = {}
for i in range(len(nums)):
a[nums[i]] = 0
for i in range(len(nums)):
a[nums[i]] += 1
# sorted 返回一个list[turple]
# turple 是以()识别的,list是[]
b = sorted(a.items(),key = lambda x:x[1],reverse = True)
print(b)
# 对 b 进行迭代遍历
p = 0
l = []
for i in range(len(b)):
if p == k:
break
l.append(b[i][0])
p += 1
return l
【python】Top K Frequent Elements
猜你喜欢
转载自blog.csdn.net/acbattle/article/details/80579710
今日推荐
周排行