1、Description

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/min-stack
 

2、Example

LeeCode Day1——有效的括号

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/min-stack

 

3、Code(Python3)
class Solution:
    def isValid(self, s: str) -> bool:
        tempList = []
        tagDic = {")": "(", "}": "{", "]": "["}
        for char in s:
            if char in tagDic:
                tag = tempList.pop() if tempList else 'tag'
                if tagDic[char] != tag:
                    return False
            else:
                tempList.append(char)
        return not tempList

 

4、Test

LeeCode Day1——有效的括号

 

相关文章:

  • 2021-09-07
  • 2021-11-05
  • 2022-01-20
  • 2021-04-30
猜你喜欢
  • 2021-10-16
  • 2022-12-23
  • 2021-05-31
  • 2021-05-30
相关资源
相似解决方案