class Solution {
public:
string removeDuplicates(string s) {
string answer;
for(auto &ch : s) {
if(answer.back() == ch) answer.pop_back();
else answer.push_back(ch);
}
return answer;
}
};
중복되는 문자열을 제거하라는 것이 이번 문제의 요약이다! 그런데 문자열을 새로운 문자열에 넣는다고 생각하면 stack자료구조를 이용할 수 있다!
Stack에 새로 넣는 것과 가장 top에 쌓여있는 것이 같다면 stack에서 꺼내는 것으로 생각하면 문제가 단순해진다. 이 코드로 빠르면 runtime이 13ms까지는 나온다 만약 auto에서 &를 이용해 복사 연산자를 사용하면 0.3 MB 정도 절약할 수 있는데 의미가 있나 싶다..ㅎ
문제: https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
Remove All Adjacent Duplicates In String - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글
[Leetcode/C++] 412. Fizz Buzz (0) | 2022.01.28 |
---|---|
[Leetcode/C++] 1154. Day of the Year (0) | 2022.01.28 |
[Leetcode/C++] 1051. Height Checker (0) | 2022.01.24 |
[Leetcode/C++] 67. Add Binary (0) | 2022.01.24 |
[Leetcode/C++] 168. Excel Sheet Column Title (0) | 2022.01.24 |