去除重复字母
分析
单调栈+贪心
class Solution {
public:
string removeDuplicateLetters(string s) {
map<char, int>mp;
map<char, int>inner;
for (char c : s) {
mp[c]++;
inner[c] = 0;
}
stack<char>st;
for (int i = 0; i < s.size(); i++) {
if (inner[s[i]]) {
mp[s[i]]--;
continue;
}
while (st.size() && s[i] < st.top() && mp[st.top()] >=1) {
inner[st.top()] = 0;
st.pop();
}
st.push(s[i]);
inner[s[i]] = 1;
mp[s[i]]--;
}
string ans;
while (st.size()) {
ans += st.top();
st.pop();
}
reverse(ans.begin(), ans.end());
return ans;
}
};