力扣(LeetCode)--20.有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。

class Solution:
def isValid(self, s: str) -> bool:
dict = {')':'(', ']':'[', '}':'{'}
stack = []
for i in s:
if i in dict and stack:
if stack[-1] == dict[i]:
stack.pop()
else:return False
else:stack.append(i)
return not stack

浙公网安备 33010602011771号