Sort Characters By Frequency

题目来源
将字符串中字符按频率排序。搞了半天,sort不会用,lambda表达式不会用…

class Solution {
public:
    string frequencySort(string s) {
        int n = s.size();
        unordered_map maps;
        for (int i=0; i maps[b] || (maps[a] == maps[b] && a < b);
        });
        return s;
    }
};

又看了看别的大神们的做法,直接先哈希一遍,然后搞一个buckets,存储出现频次为i的字符,然后再从大到小相加,得到结果,厉害,只要O(n)复杂度,不需要sort,代码如下:

class Solution {
public:
    string frequencySort(string s) {
        int n = s.size();
        unordered_map maps;
        for (int i=0; i maps[b] || (maps[a] == maps[b] && a < b);
        });
        return s;
    }
};

你可能感兴趣的:(Sort Characters By Frequency)