栈!
遍历整个字符串,遇到左括号就入栈,然后遇到和栈顶对应的右括号就出栈,遍历结束后,如果栈为空,就表示全部匹配。
list= []
遍历整个字符串,
for single in s :
print single
遇到左括号就入栈,
if s=='(' or '{' or '['
list.append(s)
然后遇到和栈顶对应的右括号就出栈,
栈顶
else:
if list.pop()='(' and s==')'
elif list.pop()='{' and s=='}'
elif list.pop()='[' and s==']'
list.pop(s)
遍历结束后,如果栈为空,就表示全部匹配。
if list==[]
return true
class Solution(object):
def isValid(self, s):
if len(s)==0:
return True
list = []
for single in s:
if single == '(' or single == '{' or single == '[':
list.append(single)
else:
if len(list) == 0:
return False
else:
temp = list.pop()
if single == ')':
if temp != '(':
return False
elif single =='}' :
if temp != '{':
return False
elif single == ']':
if temp != '[' :
return False
return True if len(list) == 0 else False