leetcode 921. 使括号有效的最少添加(Python)

时间:2023-03-10 06:44:17
leetcode 921. 使括号有效的最少添加(Python)
 class Solution:
def minAddToMakeValid(self, S):
"""
:type S: str
:rtype: int
"""
stack = []
S = list(S)
for s in S:
if len(stack) != 0:
if s == ")" and stack[-1] == "(":
stack.pop()
else:
stack.append(s)
else:
stack.append(s)
return len(stack)

击败了百分之百的人,纪念一下。

leetcode 921. 使括号有效的最少添加(Python)