921. Minimum Add to Make Parentheses Valid [Medium] 栈

921. Minimum Add to Make Parentheses Valid

921. Minimum Add to Make Parentheses Valid
class Solution(object):
    def minAddToMakeValid(self, S):
        """
        :type S: str
        :rtype: int
        """
        stack = []
        for i in S:
            if i == "(":
                stack.append(i)
            else:
                if not stack or stack[-1] != "(":
                    stack.append(i)
                else:
                    stack.pop()
        return len(stack)

你可能感兴趣的:(921. Minimum Add to Make Parentheses Valid [Medium] 栈)