一、leetcode地址

https://leetcode.com/problems/valid-parentheses/

二、问题描述

DataWhale:leetcode Day5

三、代码实现

语言: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

四、运行结果

DataWhale:leetcode Day5

相关文章:

  • 2021-09-27
  • 2021-10-22
  • 2022-01-10
  • 2021-06-20
  • 2021-07-01
猜你喜欢
  • 2021-12-06
  • 2021-08-04
相关资源
相似解决方案