题解 | #字符串出现次数的TopK问题#

字符串出现次数的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: List[str], k: int) -> List[List[str]]:
        res={}
        rel=[]
        # write code here
        for string in strings:
            if string in res:
                res[string]+=1
            else:
                res[string]=1
        for re in res.keys():
            rel.append([re,res[re]])
        def myfun(e):
            return -e[1],e[0]
        rel.sort(key=(myfun))
        return rel[0:k]
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务