美文网首页
LeetCode 20. Valid Parentheses 有

LeetCode 20. Valid Parentheses 有

作者: singed | 来源:发表于2018-08-15 15:37 被阅读0次

要求

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

有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。

相关代码

class Solution(object):
    def isValid(self, s):
        while True:
            s1 = s
            s = s.replace('[]','').replace('()','').replace('{}','')
            if s1 == s:
                if s:
                    return False
                else:
                    return True

心得体会

  1. {}()[]并不需要转义,常见的需要转义的字符有单引号,双引号和反斜杠。
  2. while True一定要考虑好退出循环条件。

相关文章

网友评论

      本文标题:LeetCode 20. Valid Parentheses 有

      本文链接:https://www.haomeiwen.com/subject/cjjwbftx.html