使括号有效的最少添加
https://www.lintcode.com/problem/1721/description?utm_source=sc-libao-ql
输入: "())"输出: 1输入: "((("输出: 3输入: "()"输出: 0输入: "()))(("输出: 4Last updated
https://www.lintcode.com/problem/1721/description?utm_source=sc-libao-ql
输入: "())"输出: 1输入: "((("输出: 3输入: "()"输出: 0输入: "()))(("输出: 4Last updated
class Solution:
"""
@param s: the given string
@return: the minimum number of parentheses we must add
"""
def min_add_to_make_valid(self, s: str) -> int:
# Write your code here
l,r=0,0
for c in s:
if c == '(':
l += 1
elif c == ')':
if l > 0:
l-=1
else:
r += 1
return l+r