题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
class Solution:
def isValid(self , s: str) -> bool:
# write code here
stack = []
d = {
'(':')',
'[':']',
'{':'}'
}
for char in s:
if char in '([{':
stack.append(d[char])
else:
if stack == [] or stack.pop() != char:
return False
return stack == []
def isValid(self , s: str) -> bool:
# write code here
stack = []
d = {
'(':')',
'[':']',
'{':'}'
}
for char in s:
if char in '([{':
stack.append(d[char])
else:
if stack == [] or stack.pop() != char:
return False
return stack == []