반응형
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
바깥 괄호를 제거하는 문제다.
(일때 시작하는 괄호인지
)일때 마지막 괄호인지를 확인하면 된다,
class Solution {
public:
string removeOuterParentheses(string S) {
string ans = "";
int cnt = 0;
for(int i=0;i<S.size();i++){
if(S[i]=='(')
cnt++;
else if (S[i]==')')
cnt--;
if(S[i]=='(' && cnt != 1)
ans+=S[i];
if(S[i]==')' && cnt !=0)
ans+=S[i];
}
return ans;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
30★★] Leetcode 1010. Pairs of Songs With Total Durations Divisible by 60 (0) | 2020.07.10 |
---|---|
29★] Leetcode 1013. Partition Array Into Three Parts With Equal Sum (0) | 2020.07.07 |
27] Leetcode 1266. Minimum Time Visiting All Points (0) | 2020.07.06 |
26★] Leetcode 1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2020.07.05 |
25★] Leetcode 1496. Path Crossing (0) | 2020.07.05 |
댓글