Day 9
Date: October 4, 2022 9:57 AM
LinkedIn: https://leetcode.cn/problems/minimum-add-to-make-parentheses-valid/
Title: 使括号有效的最少添加
用列表模拟栈
class Solution:
def minAddToMakeValid(self, s: str) -> int:
stack = []
for ch in s:
if ch==')' and stack and stack[-1]=='(': #右括号 与 非空的栈中左括号匹配
stack.pop()
else:
stack.append(ch) #不匹配入栈
return len(stack)