Leetcode 451. 根据字符出现频率排序(DAY 263)---- 后端面试题

阅读 32

2022-03-12

文章目录


原题题目


在这里插入图片描述


代码实现(首刷自解)


class Solution {
public:
    string frequencySort(string s) {
        unordered_map<char,int> map;
        priority_queue<pair<int,char>> queue;

        for(const auto& chr:s)
            ++map[chr];
            
        for(const auto& pair:map)
            queue.emplace(pair.second,pair.first);

        string ret;
        while(!queue.empty())
        {
            auto times = queue.top().first;
            auto chr = queue.top().second;
            queue.pop();
            while(times--)
                ret += chr;
        }

        return ret;
    }
};

精彩评论(0)

0 0 举报