有效的括号
程序员文章站
2022-06-17 19:49:30
...
Python
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
left = ['(', '{', '[']
right = [')', '}', ']']
match = {
')' : '(',
'}': '{',
']': '['
}
for i in s:
if not stack and (i in right):
return False
if stack and (i in right):
temp = stack.pop()
if match[i] == temp:
continue
else:
return False
if i in left:
stack.append(i)
if not stack:
return True
return False