当前位置:网站首页>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
hashmap
in - 2. then
hashmap
The letter stored in and the number of times the letter appears are transferred to the listres
in , It is convenient to sort the number of occurrences from more to less , After finishing the sequence ,res
According to the number of letters in the order from high to low - 3. Re traversal
res
, Put each lettery[0]
stayans
The 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
边栏推荐
- One brush 313 sword finger offer 06 Print linked list from end to end (E)
- Master vscode remote GDB debugging
- shell_2
- 腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上
- 捡起MATLAB的第(4)天
- 运维流程有多重要,听说一年能省下200万?
- Method 2 of drawing ROC curve in R language: proc package
- Grbl learning (I)
- Unity Shader学习
- C language self compiled string processing function - string segmentation, string filling, etc
猜你喜欢
Merging of Shanzhai version [i]
Configuration of multi spanning tree MSTP
Do we media make money now? After reading this article, you will understand
Load Balancer
腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上
volatile的含义以及用法
MySQL optimistic lock to solve concurrency conflict
Filter usage of spark operator
Partitionby of spark operator
Spark 算子之filter使用
随机推荐
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
Ice -- source code analysis
糖尿病眼底病变综述概要记录
Filter usage of spark operator
Timing model: gated cyclic unit network (Gru)
Homewbrew installation, common commands and installation path
[key points of final review of modern electronic assembly]
Leetcode-374 guess the size of the number
js正則判斷域名或者IP的端口路徑是否正確
建设星际计算网络的愿景
TIA博图——基本操作
一文读懂串口及各种电平信号含义
How can poor areas without networks have money to build networks?
linux上启动oracle服务
Win11/10家庭版禁用Edge的inprivate浏览功能
撿起MATLAB的第(9)天
Master vscode remote GDB debugging
新动态:SmartMesh和MeshBox的合作新动向
王启亨谈Web3.0与价值互联网“通证交换”
Go language, condition, loop, function