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)

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

posted on 2018-12-09 15:37  小明明明不是企鹅  阅读(546)  评论(0编辑  收藏  举报