排序LeetCode 451 Sort Characters By Frequency
程序员文章站
2022-05-26 22:25:57
...
LeetCode 451
Sort Characters By Frequency
- Problem Description:
根据字符串中每个字符出现频率从高到低组成新的字符串。
具体的题目信息:
https://leetcode.com/problems/sort-characters-by-frequency/description/ - Example:
- Solution:
class Solution {
public:
static bool cmp(pair<char, int>& p, pair<char, int>& q) {
return p.second>q.second;
}
string frequencySort(string s) {
if (s.length()==0||s.length()==1||s.length()==2) return s;
string str = "";
map<char, int> character;
for (int i = 0; i < s.length(); i++) {
character[s[i]]++;
}
vector<pair<char, int>> v(character.begin(), character.end());
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); i++) {
while(v[i].second--) {
str += v[i].first;
}
}
return str;
}
};
上一篇: 排序算法---直接插入排序(3)
下一篇: Java学习--排序算法之插入法排序
推荐阅读
-
451. Sort Characters By Frequency
-
排序LeetCode 451 Sort Characters By Frequency
-
【Leetcode】451. 根据出现的频率对字符排序(Sort Characters By Frequency)
-
LeetCode 451. 根据字符出现频率排序(Sort Characters By Frequency)
-
LeetCode | 0451. Sort Characters By Frequency根据字符出现频率排序【Python】
-
LeetCode 451. Sort Characters By Frequency 按照字符频率排序(Java实现)
-
LeetCode 22 May, Sort Characters By Frequency, 按字符出现频率排序字符串
-
Leetcode 451.根据字符出现频率排序(Sort Characters By Frequency)
-
451 Sort Characters By Frequency 根据字符出现频率排序
-
Sort Characters By Frequency 根据字符出现频率排序