class Solution {
public:
string removeOuterParentheses(string s) {
int idx= 0;
string answer = "";
for(int i = 0; i < s.length(); i++) {
if(s[i] == '\(') {
if(idx != 0) answer.push_back(s[i]);
idx++;
}
else {
if(idx > 1) answer.push_back(s[i]);
idx--;
}
}
return answer;
}
};
그냥 O(n)으로 최대한 처리하고 싶어서 처음 괄호와 끝나는 괄호를 제외하고 모두 담도록 구현을 해보았습니다.
문제: https://leetcode.com/problems/remove-outermost-parentheses/
Remove Outermost Parentheses - 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++] 206. Reverse Linked List (0) | 2022.01.24 |
---|---|
[Leetcode/C++] 496. Next Greater Element 1 (0) | 2022.01.24 |
[Leetcode/C++] 551. Student Attendance Record I (0) | 2022.01.24 |
[LeetCode/C++] 14. Longest Common Prefix (0) | 2022.01.23 |
[LeetCode/C++] 844. Backspace String Compare (0) | 2022.01.20 |