본문 바로가기
IT/알고리즘

28] Leetcode 1021. Remove Outermost Parentheses

by 깻잎쌈 2020. 7. 6.
반응형

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;
        
    }
};
반응형

댓글