一、leetcode地址
https://leetcode.com/problems/valid-parentheses/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def isValid(self, s: str) -> bool:
stack = []
mapping = {')':'(',']':'[','}':'{'}
for p in s:
if p in '([{':
stack.append(p)
elif p in ')]}':
top_element = stack.pop() if stack else '#'
if top_element != mapping[p]:
return False
break
return not stack