给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次。你必须保证返回结果是所有可能结果中的以字典排序的最短结果。
例如:
给定 "bcabc"
返回 "abc"
给定 "cbacdcbc"
返回 "acdb"

详见:https://leetcode.com/problems/remove-duplicate-letters/description/

C++:

class Solution {
public:
    string removeDuplicateLetters(string s) {
        int m[256]={0},visited[256]={0};
        string res="0";
        for(char a:s)
        {
            ++m[a];
        }
        for(char a:s)
        {
            --m[a];
            if(visited[a])
            {
                continue;
            }
            while(a<res.back()&&m[res.back()])
            {
                visited[res.back()]=0;
                res.pop_back();
            }
            res+=a;
            visited[a]=1;
        }
        return res.substr(1);
    }
};

 详见:http://www.cnblogs.com/grandyang/p/5085379.html

相关文章:

  • 2021-09-17
  • 2021-11-08
  • 2021-11-20
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
猜你喜欢
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-05
相关资源
相似解决方案