欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LC.921. Minimum Add to Make Parentheses Valid

程序员文章站 2022-03-10 08:18:12
...

LC.921. Minimum Add to Make Parentheses Valid

class Solution:
    """
    括号匹配 
    看剩下多少了
    """
    def minAddToMakeValid(self, S: str) -> int:
        stack = []
        for char in S:
            if char == "(":
                stack.append(char)
            else:
                if len(stack) and stack[-1] == "(":
                    stack.pop()
                else:
                    stack.append(char)

        return len(stack)