Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save icameling/e43c52567af800c0f704db8e6541675d to your computer and use it in GitHub Desktop.
Save icameling/e43c52567af800c0f704db8e6541675d to your computer and use it in GitHub Desktop.
#栈与队列 #删除字符串中的所有相邻重复项
class Solution {
public:
string removeDuplicates(string s) {
stack<char> str_ret;
for (int i = 0; i < s.size(); ++i) {
if (!str_ret.empty() && s[i] == str_ret.top()) {
str_ret.pop();
} else {
str_ret.push(s[i]);
}
}
string ret;
ret.resize(str_ret.size());
for (int i = str_ret.size() - 1; i >= 0; --i) {
ret[i] = str_ret.top();
str_ret.pop();
}
return ret;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment