C++ using int[256]


  • 0
    G

    class Solution {
    public:
    string frequencySort(string s) {
    string res;
    vector<int> cnt(256,0);
    for (int i=0; i<256; ++i){
    cnt[i] = i;
    }
    for (int i=0; i<s.size(); ++i){
    cnt[s[i]] += 256;
    }
    sort(cnt.begin(), cnt.end());
    for(int i=255; i>=0; --i){
    char ch = cnt[i]%256;
    cnt[i]/=256;
    while(cnt[i]--){
    res.push_back(ch);
    }
    }
    return res;
    }
    };


  • 0
    G

    soooorrry, i don't know how to make it intented
    class Solution {
    public:
    string frequencySort(string s) {
    string res;
    vector<int> cnt(256,0);
    for (int i=0; i<256; ++i){
    cnt[i] = i;
    }
    for (int i=0; i<s.size(); ++i){
    cnt[s[i]] += 256;
    }
    sort(cnt.begin(), cnt.end());
    for(int i=255; i>=0; --i){
    char ch = cnt[i]%256;
    cnt[i]/=256;
    while(cnt[i]--){
    res.push_back(ch);
    }
    }
    return res;
    }
    };


Log in to reply
 

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