当前位置:网站首页>Sort by character occurrence frequency 451
Sort by character occurrence frequency 451
2022-04-23 16:02:00 【Zhang Joshua】
451. Sort the characters according to their frequency of occurrence
- Question no : Power button 451
- Knowledge point : Hashtable , Count
- Goal completion :25/150
- summary
stem :

Ideas :
- 1. First save the letter in the string and the number of times the letter appears in the dictionary
hashmapin - 2. then
hashmapThe letter stored in and the number of times the letter appears are transferred to the listresin , It is convenient to sort the number of occurrences from more to less , After finishing the sequence ,resAccording to the number of letters in the order from high to low - 3. Re traversal
res, Put each lettery[0]stayansThe number of times the letter appears is appended to they[1]Time
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
版权声明
本文为[Zhang Joshua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231558306122.html
边栏推荐
- TIA botu - basic operation
- Read the meaning of serial port and various level signals
- Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
- 【现代电子装联期末复习要点】
- Redis master-slave replication process
- MySQL - MySQL查询语句的执行过程
- C language --- advanced pointer
- Deletes the least frequently occurring character in the string
- JS regular détermine si le nom de domaine ou le chemin de port IP est correct
- 捡起MATLAB的第(5)天
猜你喜欢

捡起MATLAB的第(8)天

Pgpool II 4.3 Chinese Manual - introductory tutorial

捡起MATLAB的第(5)天

One brush 314 sword finger offer 09 Implement queue (E) with two stacks

Meaning and usage of volatile

Do we media make money now? After reading this article, you will understand

matplotlib教程05---操作图像

Master vscode remote GDB debugging

How can poor areas without networks have money to build networks?

Spark 算子之distinct使用
随机推荐
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
linux上启动oracle服务
VIM specifies the line comment and reconciliation comment
Day (10) of picking up matlab
js正則判斷域名或者IP的端口路徑是否正確
JVM - Chapter 2 - class loader subsystem
Pgpool II 4.3 Chinese Manual - introductory tutorial
OAK-D树莓派点云项目【附详细代码】
gps北斗高精度卫星时间同步系统应用案例
Using JSON server to create server requests locally
Groupby use of spark operator
建设星际计算网络的愿景
Nanny Anaconda installation tutorial
Spark 算子之coalesce与repartition
5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
通过Feign在服务之间传递header请求头信息
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
【自娱自乐】构造笔记 week 2
C language --- advanced pointer
Day (5) of picking up matlab