其他分享
首页 > 其他分享> > 1021. Remove Outermost Parentheses

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)

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

 

标签:Parentheses,1021,opened,Outermost,str,res,removeOuterParentheses,append
来源: https://blog.csdn.net/weixin_40801853/article/details/89287252