class Solution {
public:
string removeDuplicates(string s) {
if (s.size() < 2) return s;
stack<char> t;
for (int i = 0; i < s.size(); ++i) {
if (t.empty()) t.push(s[i]);
else {
if (s[i] == t.top()) {
t.pop();
continue;
} else {
t.push(s[i]);
}
}
}
string res = "";
while (!t.empty()) {
res = t.top() + res;
t.pop();
}
return res;
}
};
? ? ? ? 时间复杂度高
class Solution {
public:
string removeDuplicates(string s) {
string res = "";
for (auto it : s) {
if (res.empty() || it != res.back()) {
res.push_back(it);
} else {
res.pop_back();
}
}
return res;
}
};
? ? ? ? 写完代码多思考怎么优化