字典存储 字典排序 遍历排序后的list
class Solution:
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
dict1={}
for c in s:
if c in dict1:
dict1[c]+=1
else:
dict1[c]=1
b=sorted(dict1.items(),key=lambda x:x[1],reverse=True)
res=''
for item in b:
res+=item[0]*item[1]
return res

本文介绍了一种使用字典存储字符频率,并通过排序字典项实现字符串中字符按出现频率从高到低排序的方法。该方法首先遍历字符串,统计每个字符的出现次数,然后将字典项按值(即字符频率)逆序排序,最后根据排序后的字典项构造结果字符串。
777

被折叠的 条评论
为什么被折叠?



