题解 | #字符统计#

字符统计

http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

while True:
    try:
        s = input()
        # 统计字符次数
        dic = {}
        for c in s:
            if c not in dic:
                dic[c] = 1
            else:
                dic[c] += 1
        lst = list(dic.keys())
        # 冒泡排序
        for i in range(len(lst)-1):
            for j in range(i+1, len(lst)):
            	# 次数降序排列
                if dic[lst[i]] < dic[lst[j]]:
                    lst[i], lst[j] = lst[j], lst[i]
                elif dic[lst[i]] == dic[lst[j]]:
                	# ASCII升序排列
                    if lst[i] > lst[j]:
                        lst[i], lst[j] = lst[j], lst[i]
        print(''.join(lst))
    except:
        break
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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