当前位置:网站首页>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
边栏推荐
猜你喜欢
糖尿病眼底病变综述概要记录
Install redis and deploy redis high availability cluster
Matplotlib tutorial 05 --- operating images
CVPR 2022 quality paper sharing
[key points of final review of modern electronic assembly]
C language self compiled string processing function - string segmentation, string filling, etc
捡起MATLAB的第(5)天
Day (4) of picking up matlab
The principle and common methods of multithreading and the difference between thread and runnable
Grbl learning (I)
随机推荐
Day (6) of picking up matlab
Sortby use of spark operator
Temporal model: long-term and short-term memory network (LSTM)
保姆级Anaconda安装教程
Compile, connect -- Notes
R语言中实现作图对象排列的函数总结
Spark 算子之coalesce与repartition
Master vscode remote GDB debugging
Homewbrew installation, common commands and installation path
Deletes the least frequently occurring character in the string
Day (5) of picking up matlab
shell_ two
Named in pytoch_ parameters、named_ children、named_ Modules function
Treatment of idempotency
[key points of final review of modern electronic assembly]
js正则判断域名或者IP的端口路径是否正确
Partitionby of spark operator
How important is the operation and maintenance process? I heard it can save 2 million a year?
Filter usage of spark operator
撿起MATLAB的第(9)天