python中list就可以直接当栈用
1 class Solution(object): 2 def isValid(self, s): 3 """ 4 :type s: str 5 :rtype: bool 6 """ 7 length = len(s) 8 help_stack = [] 9 i = 0 10 while i < length: 11 if s[i] == '(' or s[i] == '{' or s[i] == '[': 12 help_stack.append(s[i]) 13 elif s[i] == ')': 14 if len(help_stack) != 0 and help_stack[-1] == '(': 15 help_stack.pop() 16 else: 17 return False 18 elif s[i] == '}': 19 if len(help_stack) != 0 and help_stack[-1] == '{': 20 help_stack.pop() 21 else: 22 return False 23 elif s[i] == ']': 24 if len(help_stack) != 0 and help_stack[-1] == '[': 25 help_stack.pop() 26 else: 27 return False 28 i += 1 29 if len(help_stack) == 0 : 30 return True 31 else: 32 return False