如果当前栈为空,那么再看当前符号为右半部分,则一定不合法;否则就入栈;
如果当前栈不为空,那么判断当前符号能否和栈尾配对,能配对则将栈尾符号出栈;不能配对则看是否##是右半##部分,如果是则一定不合法,不是则入栈;
class Solution:
def isValid(self , s ):
# write code here
ss={'{':1,'}':-1,'[':2,']':-2,'(':3,')':-3} stack=[] for x in s: if len(stack)==0: if ss[x]<0:return False else:stack.append(x) elif ss[x]+ss[stack[-1]]==0:stack.pop() elif ss[x]<0:return False else:stack.append(x) if len(stack)>0:return False else:return True