LC.921. Minimum Add to Make Parentheses Valid
程序员文章站
2022-03-10 08:18:12
...
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)