赞
踩
- class Solution(object):
- def topKFrequent(self, nums, k):
- d = {}
- res = []
- for i in nums:
- if i in d.keys():
- d[i] += 1
- else:
- d[i] = 1
- d_by_value = sorted(d.items(),key=lambda d: d[1],reverse=True)
- print d_by_value
- for i in xrange(k):
- res.append(d_by_value[i][0])
- return res
重点在于将字典按value排序
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。