R5-栈篇
匹配用哈希表
其他用栈
class Solution:
def isValid(self, s: str) -> bool:
#?的存在是保证栈不为空
dict={'{':'}','[':']','(':')','?':'?'}
stack=['?']
for c in s:
if c in dict:
stack.append(c)
elif dict[stack.pop()]!=c:
return False
return len(stack)==1
ps:
布尔值返回真的很爽