阅读背景:

基于Python3给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。--leetcode_大大怪授的博客_有效字符串python

来源:互联网 

基于Python3
20. 有效的括号

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        # 判断是否是奇数或空字符
        if len(s)<2 or len(s)%2!=0:
            if s=='':
                return True
            else:
                return False
        count = 0
        length = len(s)
        # 将其中的(){}[] 都换掉,然后判断是否有剩余
        while(count<length/2):
            s = s.replace("{}","").replace("[]","").replace("()","")
            count+=1
        if len(s)>0:
            return False
        else:
            return True
class Solution(ob



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: