题解
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack=[]
for c in s:
if c == '(':
stack.append(')')
elif c== '{':
stack.append('}')
elif c=='[':
stack.append(']')
elif stack and stack[-1]==c:
stack.pop()
else:
return False
return True if not stack else False
我的领悟
利用栈的思想,通过列表来表示,如果有左边的符号,就把右边的输进去,如果栈顶(stack[-1]为列表的最后一个元素)和右边的一样,就把栈顶的元素弹出来,最后如果全部弹出来stack为空就说明括号匹配
return True if not stack else False
写法很高级,if not stack说明 stack为空