当前位置:网站首页>451. 根据字符出现频率排序
451. 根据字符出现频率排序
2022-04-23 15:58:00 【张.Joshua】
451. 根据字符出现频率排序
- 题号:力扣451
- 知识点:哈希表,计数
- 目标完成度:25/150
- 总结
题干:
思路:
- 1.先将字符串中出现的字母和该字母出现的次数存到字典
hashmap
中 - 2.再将
hashmap
中存储的字母和该字母出现的次数转移到列表res
中,方便对出现次数由多到少排序,排完序后,res
中按照字母出现的次数由高到低排列 - 3.再遍历
res
,将每个字母y[0]
在ans
中追加该字母出现的次数y[1]
次
class Solution:
def frequencySort(self, s: str) -> str:
hashmap = {
}
for x in s:
if x in hashmap:
hashmap[x] += 1
else:
hashmap[x] = 1
res = []
for key, value in hashmap.items():
res.append([key, value])
res.sort(key=lambda arr:arr[1], reverse=True)
ans = ''
for y in res:
for i in range(y[1]):
ans += (y[0])
return ans
版权声明
本文为[张.Joshua]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44742084/article/details/124364038
边栏推荐
- Spark 算子之coalesce与repartition
- Why is IP direct connection prohibited in large-scale Internet
- leetcode-374 猜数字大小
- Cap theorem
- JS regular determines whether the port path of the domain name or IP is correct
- Sortby use of spark operator
- C language --- advanced pointer
- C语言自编字符串处理函数——字符串分割、字符串填充等
- [split of recursive number] n points K, split of limited range
- R语言中实现作图对象排列的函数总结
猜你喜欢
Application of Bloom filter in 100 million flow e-commerce system
Spark 算子之coalesce与repartition
单体架构系统重新架构
新动态:SmartMesh和MeshBox的合作新动向
糖尿病眼底病变综述概要记录
C language self compiled string processing function - string segmentation, string filling, etc
TIA博图——基本操作
Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
Coalesce and repartition of spark operators
建设星际计算网络的愿景
随机推荐
Upgrade MySQL 5.1 to 5.611
leetcode-396 旋转函数
一文掌握vscode远程gdb调试
Spark 算子之distinct使用
How can poor areas without networks have money to build networks?
Partitionby of spark operator
Temporal model: long-term and short-term memory network (LSTM)
大型互联网为什么禁止ip直连
TIA博图——基本操作
Application case of GPS Beidou high precision satellite time synchronization system
Intersection, union and difference sets of spark operators
One brush 313 sword finger offer 06 Print linked list from end to end (E)
PS为图片添加纹理
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
捡起MATLAB的第(5)天
[self entertainment] construction notes week 2
Upgrade MySQL 5.1 to 5.69
Compile, connect -- Notes
Spark 算子之交集、并集、差集
Pgpool II 4.3 Chinese Manual - introductory tutorial