[Algorithms] LeetCode 1047. 删除字符串中的所有相邻重复项
Categories Algorithms StackAndQueue
Tags
来源:代码随想录
栈模拟
注意是两两删除。
class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
for (int i = 0; i < s.size(); i++) {
if (st.empty() || st.top() != s[i]) {
st.push(s[i]);
} else {
st.pop();
}
}
string l;
while (!st.empty()) {
l += st.top();
st.pop();
}
reverse(l.begin(), l.end());
return l;
}
};
Page views: Loading... · Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号