括号匹配(python)

括号匹配(python) class Solution: def isValid(self, s: str) - bool: if len(s)%2!0: return False stack[] mp{):(,]:[,}:{} for ch in s: if ch not in mp: #说明是左括号需要入栈 stack.append(ch) #遇到右括号如果栈为空或弹出元素匹配不上返回False elif not stack or stack.pop()!mp[ch]: return False return not stack思路1.使用栈数据结构2.用一个哈希表存下映射关系3.左括号就入栈遇到右括号就匹配