for(auto c:S){
if(st.empty()){
st.push(c);
}
else {
if(st.top()==c){st.pop();}
else {st.push(c);}
}
}
class Solution {
public:
string removeDuplicates(string S) {
stack<char> st;
char ch[21000];
int i=0,n;
string ans;
for(auto c:S){
if(i==0){
ch[i]=c;
i++;
}
else {
if(ch[i-1]==c){i--;}
else {ch[i]=c;i++;}
}
}
n=i;
for(i=0;i<n;i++)ans+=ch[i];
return ans;
}
};
运行时间 | 内存消耗 |
---|---|
8ms | 10MB |
原文:https://www.cnblogs.com/edithlee/p/14507918.html