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

有效字符串需满足:

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

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

示例 1:

输入: "()"
输出: true

思路:使用栈的方法。

找工作刷题记录_007有效的括号

python语法:查找字典的时候默认查找键值key.

class Solution(Object):
      def isValid(self,s):
          stack = []
          mapping = {')':'(','}':'{',']':'['}
          for char in s:
              if char in mapping:
                 top_element = stack.pop()
                 if mapping[char] != top_element:
                    return False
              else:
                 stack.append(char)
          return no stack

 

相关文章:

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