File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 89
89
| 429| [ N 叉树的层序遍历] ( https://leetcode.cn/problems/n-ary-tree-level-order-traversal/ ) | [ JavaScript] ( ./algorithms/n-ary-tree-level-order-traversal.js ) | Medium|
90
90
| 434| [ 字符串中的单词数] ( https://leetcode.cn/problems/number-of-segments-in-a-string/ ) | [ JavaScript] ( ./algorithms/number-of-segments-in-a-string.js ) | Easy|
91
91
| 437| [ 路径总和 III] ( https://leetcode.cn/problems/path-sum-iii/ ) | [ JavaScript] ( ./algorithms/path-sum-iii.js ) | Medium|
92
+ | 451| [ 根据字符出现频率排序] ( https://leetcode.cn/problems/sort-characters-by-frequency/ ) | [ JavaScript] ( ./algorithms/sort-characters-by-frequency.js ) | Medium|
92
93
| 453| [ 最小操作次数使数组元素相等] ( https://leetcode.cn/problems/minimum-moves-to-equal-array-elements/ ) | [ JavaScript] ( ./algorithms/minimum-moves-to-equal-array-elements.js ) | Easy|
93
94
| 448| [ 找到所有数组中消失的数字] ( https://leetcode.cn/problems/find-all-numbers-disappeared-in-an-array/ ) | [ JavaScript] ( ./algorithms/find-all-numbers-disappeared-in-an-array.js ) | Easy|
94
95
| 485| [ 最大连续 1 的个数] ( https://leetcode.cn/problems/max-consecutive-ones/ ) | [ JavaScript] ( ./algorithms/max-consecutive-ones.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {string }
4
+ */
5
+ var frequencySort = function ( s ) {
6
+ // 字符计数
7
+ const map = new Map ( ) ;
8
+
9
+ for ( let char of s ) {
10
+ if ( map . has ( char ) ) {
11
+ map . set ( char , map . get ( char ) + 1 ) ;
12
+ } else {
13
+ map . set ( char , 1 ) ;
14
+ }
15
+ }
16
+
17
+ // 按照出现次数的高低排序
18
+ let result = Array . from ( map . entries ( ) ) . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] ) ;
19
+ let str = "" ;
20
+
21
+ for ( let [ char , count ] of result ) {
22
+ str += char . repeat ( count ) ;
23
+ }
24
+
25
+ return str ;
26
+ } ;
You can’t perform that action at this time.
0 commit comments