Python:
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = [None] #用来依次存储左括号
#用栈来操作
dictt = {']':'[' , '}':'{' , ')':'('}
for i in s:
if i in dictt: #判断当前字符是左括号还是右括号
#如果是右括号,判断与之前的左括号是否成对,不成对的话就返回False
if dictt[i] != stack.pop():
return False
else:
stack.append(i) #如果是左括号,就放入stack中
return stack == [None]