#
# return topK string
# @param strings string字符串一维数组 strings
# @param k int整型 the k
# @return string字符串二维数组
#
class Solution:
def topKstrings(self , strings , k ):
# write code here
dict={}
for i in range(len(strings)):
if strings[i] not in dict:
dict[strings[i]]=1
else:
dict[strings[i]] +=1
return (sorted(dict.items(),key=lambda x:(-x[1],x[0]))[:k]) 先转化为字典
对字典进行排序,排序函数要记住,数字排序时想要按照反序排列可以加一个负号



京公网安备 11010502036488号