Created
July 19, 2022 13:48
-
-
Save icameling/e43c52567af800c0f704db8e6541675d to your computer and use it in GitHub Desktop.
#栈与队列 #删除字符串中的所有相邻重复项
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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