string removeDuplicates(string str)
{
stack<char> s;
auto begin = str.begin(), end = str.end();
while (begin != end)
{
auto ch = *begin;
if (!s.empty() &&
s.top() == ch)
s.pop();
else
s.push(ch);
begin++;
}
str.clear();
while (!s.empty())
{
str.push_back(s.top());
s.pop();
}
reverse(str.begin(), str.end());
return str;
}
'코딩테스트 > LeetCode' 카테고리의 다른 글
[LeetCode] Majority Element (0) | 2023.12.19 |
---|---|
[LeetCode] Remove All Adjacent Duplicates in String II (0) | 2023.12.12 |
[LeetCode] Valid Palindrome II (0) | 2023.12.11 |
[LeetCode] Excel Sheet Column Number (0) | 2023.12.11 |
[LeetCode] Evaluate Reverse Polish Notation (0) | 2023.12.11 |