[leetcode] remove duplicate letters

好懒again,草稿

用时4ms,内存1M (好高啊魂淡

class Solution {
public:
    string removeDuplicateLetters(string s) {
        auto length=s.size();
        if (length<=1)
            return s;
        int counts[26]={0};
        for (int i=0;i<length;i++)
            ++counts[s[i]-'a'];
        int smallest_ch_index=0;
        char smallest_ch=s[0];
        for (int i=0;i<length;i++) {
            if (s[i]<smallest_ch) {
                smallest_ch_index=i;
                smallest_ch=s[i];
            }
            if(--counts[s[i]-'a']==0)
                break;
        }
        string remained=s.substr(smallest_ch_index+1);
        while (remained.find(smallest_ch)!=string::npos)
            remained=remained.erase(remained.find(smallest_ch),1);
        return smallest_ch+removeDuplicateLetters(remained);
    }
};

 

上一篇:INSERT 中ON DUPLICATE KEY UPDATE的使用


下一篇:Duplicate entry '97112' for key 1