当前位置:网站首页>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
边栏推荐
- 王启亨谈Web3.0与价值互联网“通证交换”
- MySQL - execution process of MySQL query statement
- How important is the operation and maintenance process? I heard it can save 2 million a year?
- JS regular détermine si le nom de domaine ou le chemin de port IP est correct
- 单体架构系统重新架构
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- Load Balancer
- Upgrade MySQL 5.1 to 5.610
- API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
- New developments: new trends in cooperation between smartmesh and meshbox
猜你喜欢

5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库

Spark 算子之groupBy使用

IronPDF for .NET 2022.4.5455

Intersection, union and difference sets of spark operators

Implement default page

C#,贝尔数(Bell Number)的计算方法与源程序
![[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope](/img/39/4d37c381f16dbe11acc64c92d0a1e5.png)
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope

Function summary of drawing object arrangement in R language

New developments: new trends in cooperation between smartmesh and meshbox

Spark 算子之sortBy使用
随机推荐
捡起MATLAB的第(8)天
Website pressure measurement tools Apache AB, webbench, Apache jemeter
Upgrade MySQL 5.1 to 5.66
Day (8) of picking up matlab
leetcode-396 旋转函数
Function summary of drawing object arrangement in R language
一文读懂串口及各种电平信号含义
MySQL - MySQL查询语句的执行过程
Why disable foreign key constraints
通过Feign在服务之间传递header请求头信息
C language --- advanced pointer
GRBL学习(二)
【自娱自乐】构造笔记 week 2
Spark 算子之distinct使用
捡起MATLAB的第(10)天
捡起MATLAB的第(9)天
Jour (9) de ramassage de MATLAB
Load Balancer
Codejock Suite Pro v20. three
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention