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)

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

\"\"

收藏 打印