题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
class Solution:
def isValid(self, s: str) -> bool:
# write code here
if len(s) % 2:
return False
ck_stack = []
ck_dict = {
"}": "{",
"]": "[",
")": "(",
}
for i in s:
if i not in ck_dict:
ck_stack.append(i)
elif not ck_stack or ck_stack[-1] != ck_dict[i]:
return False
else:
ck_stack.pop()
return not ck_stack
