C++map值排序
class Solution {
public:
static bool cmp(pair<char,int> a, pair<char,int> b){
return a.second>b.second;
}
string frequencySort(string s) {
string ans="";
map<char,int> mp;
for(auto c:s){
mp[c]++;
}
vector<pair<char,int>> vs;
for(auto kv:mp){
vs.push_back({kv.first,kv.second});
}
sort(vs.begin(),vs.end(),cmp);
for(auto kv:vs){
for(int i=0;i<kv.second;i++){
ans+=kv.first;
}
}
return ans;
}
};