重点:完全匹配类的题目可以第一时间想到栈
第一种解法:直接用栈
class Solution {
public:
string removeDuplicates(string s) {
stack<char>st;
for(int i=0;i<s.size();i++){
if(st.empty()||s[i]!=st.top())
st.push(s[i]);
else
st.pop();
}
string result="";
while(!st.empty()){
result+=st.top();
st.pop();
}
reverse(result.begin(),result.end());
return result;
}
};
第二种解法:用字符串模拟栈的操作
push_back:往字符串后面插入元素
pop_back:移出字符串最后一个元素
back:输出字符串最后一个元素,但不移出。