1047. Remove All Adjacent Duplicates In String 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021class Solution {public: string removeDuplicates(string s) { stack<char> sta; int n = s.size(); for(int i=0;i<n;i++){ if(i+1< n && s[i] == s[i+1]) i++; else{ if(!sta.empty() && sta.top()==s[i]) sta.pop(); else sta.push(s[i]); } } string ret; while(!sta.empty()){ ret+=sta.top(); sta.pop(); } reverse(ret.begin(), ret.end()); return ret; }}; 1234567891011121314151617class Solution {public: string removeDuplicates(string s) { string ret; stack<char> sta; for(char c:s){ if(!sta.empty() && sta.top() == c) sta.pop(); else sta.push(c); } while(!sta.empty()){ ret+=sta.top(); sta.pop(); } reverse(ret.begin(), ret.end()); return ret; }}; option 2 simplify option 1 without stack1234567891011class Solution {public: string removeDuplicates(string s) { string ret; for(char c:s){ if(!ret.empty() && ret.back() == c) ret.pop_back(); else ret+=c; } return ret; }}; analysis time complexity O(n) space complexity O(n)