其他分享
首页 > 其他分享> > 316. 去除重复字母

316. 去除重复字母

作者:互联网

题面:

 

 题解:单调栈

代码:

class Solution {
public:
    string removeDuplicateLetters(string s) {
             int n = s.size();
             vector<int> num(26,0),vis(26,0);
             string stk;
             for(int i = 0; i < n; i ++)
             {
                     num[s[i] - 'a'] ++;
             }
             for(int i = 0;i < n; i++  )
             {
                  if(!vis[s[i] - 'a'])
                  {
                      while(stk.size()&&stk.back() > s[i]&&num[stk.back() - 'a'] > 0)
                      {
                                vis[stk.back() - 'a'] = 0;
                                stk.pop_back();
                      }
                      vis[s[i] - 'a'] = 1;
                      stk.push_back(s[i]);
                  }
                  num[s[i] - 'a' ] --;
             }
             return stk;
    }
};

 

标签:string,int,字母,back,stk,316,vis,num,去除
来源: https://www.cnblogs.com/flyljz/p/15847974.html