1 class Solution:
 2     def minAddToMakeValid(self, S):
 3         """
 4         :type S: str
 5         :rtype: int
 6         """
 7         stack = []
 8         S = list(S)
 9         for s in S:
10             if len(stack) != 0:
11                 if s == ")" and stack[-1] == "(":
12                     stack.pop()
13                 else:
14                     stack.append(s)
15             else:
16                 stack.append(s)
17         return len(stack)

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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

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

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄