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

 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)

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

猜你喜欢

转载自www.cnblogs.com/woshizhizhang/p/10091429.html
今日推荐