leetcode答案 有效的括号(python)

 1 def isValid(s):
 2     if s and len(s) % 2 is 0:
 3         a = {')': '(', ']': '[', '}': '{'}
 4         l = [None]
 5         for i in s:
 6             if i in a and a[i] == l[-1]:
 7                 l.pop()
 8             else:
 9                 l.append(i)
10         return len(l) == 1
11     elif len(s) % 2 is 1:
12         return False
13     else:
14         return True

 

posted @ 2019-02-21 17:36  超凡-  阅读(149)  评论(0编辑  收藏  举报