C++ use lambda sort


  • 0
    W
    class Solution {
    public:
        string frequencySort(string s) {
            unordered_map<char,int>mp;
            int n = s.length();
            if(n==0)
                return s;
            for(int i = 0 ; i < n ; ++i){
                mp[s[i]]++;
            }
            vector<pair<char,int>> vv(mp.begin(),mp.end());
            auto lamda = [&](const pair<char,int> & l, const pair<char,int>& r){ return l.second > r.second;};
            sort(vv.begin(),vv.end(),lamda);
            string res = "";
            for(auto& c:vv){
                int i = 0;
                for(int j = 0 ; j < c.second ; ++j)
                    res+=c.first;
            }
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.