题解 | #判断一个链表是否为回文结构#
字符串出现次数的TopK问题
http://www.nowcoder.com/practice/fd711bdfa0e840b381d7e1b82183b3ee
#
# 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]) 先转化为字典
对字典进行排序,排序函数要记住,数字排序时想要按照反序排列可以加一个负号
文远知行公司福利 583人发布