思想:
1.定义一个列表valid存放着每对有效符号
2.将字符串s中的字符放入stack
3.判断stack的长度是否大于等于2并且stack[-2]+stack[-1]是否在valid中。若是则跳转4,否则跳转2
4.将stack[-1]和stack[-2]删除,并跳转2
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
valid =['()','[]','{}']
for i in range(0,len(s)):
stack.append(s[i])
if len(stack)>=2 and stack[-2]+stack[-1] in valid:
stack.pop()
stack.pop()
return len(stack)==0