Valid Parentheses
Question
Answer
solution:
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack, lookup = [], {"(": ")", "{": "}", "[": "]"}
for parenthese in s:
if parenthese in lookup:
stack.append(parenthese)
elif len(stack) == 0 or lookup[stack.pop()] != parenthese: #用lookup[stack.pop()]来判断前一个符号对应的结尾符号是否与当前符号一致
return False
return len(stack) == 0Knowledge:
Last updated