LeetCode 1047. 删除字符串中的所有相邻重复项
class Solution {
public:
string removeDuplicates(string s) {
stack<char> stack;
for (int i = 0; i < s.size(); i ++) {
if (stack.empty() || s[i] != stack.top()) stack.push(s[i]);
else stack.pop();
}
string res = "";
while (!stack.empty()) res += stack.top(), stack.pop();
reverse(res.begin(), res.end());
return res;
}
};

浙公网安备 33010602011771号