1021. Remove Outermost Parentheses

class Solution(object):
    def removeOuterParentheses(self, S):
        """
        :type S: str
        :rtype: str
        """
        res, opened = [], 0
        for c in S:
            if c == '(' and opened > 0:
                res.append(c)
            if c == ')' and opened > 1:
                res.append(c)
            opened += 1 if c =='(' else -1
        return "".join(res)

除掉最外层的括号就是剩下的答案 

 

上一篇:LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号


下一篇:PAT 1021 Deepest Root