当前位置:网站首页>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
边栏推荐
- Win11/10家庭版禁用Edge的inprivate浏览功能
- utils. Deprecated in35 may be cancelled due to upgrade. What should I do
- Partitionby of spark operator
- ESP32_ Arduino
- One brush 313 sword finger offer 06 Print linked list from end to end (E)
- Named in pytoch_ parameters、named_ children、named_ Modules function
- The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?
- Go language, condition, loop, function
- Extract non duplicate integers
- Grbl learning (I)
猜你喜欢
Day (9) of picking up matlab
matplotlib教程05---操作图像
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
Ice -- source code analysis
Spark 算子之distinct使用
【现代电子装联期末复习要点】
Do we media make money now? After reading this article, you will understand
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
Merging of Shanzhai version [i]
Redis master-slave replication process
随机推荐
Go language slice, range, set
Day (8) of picking up matlab
[self entertainment] construction notes week 2
MySQL optimistic lock to solve concurrency conflict
TIA博图——基本操作
Groupby use of spark operator
New developments: new trends in cooperation between smartmesh and meshbox
C language self compiled string processing function - string segmentation, string filling, etc
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
Day (3) of picking up matlab
捡起MATLAB的第(8)天
GRBL学习(一)
捡起MATLAB的第(3)天
Extract non duplicate integers
Intersection, union and difference sets of spark operators
GRBL学习(二)
Spark 算子之sortBy使用
实现缺省页面
451. 根据字符出现频率排序
贫困的无网地区怎么有钱建设网络?