20.有效的括号(通过)Python

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


class Solution:
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        vl = {'{':1,'}':-1,'[':2,']':-2,'(':3,')':-3}
        y = list(s)
        llong = len(y)
        result = []
        for i in range(llong):
            if vl[y[i]] > 0:
                result.append(vl[y[i]])
            else:
                if len(result) == 0:
                    return False
                elif (result[len(result)-1] + vl[y[i]]) != 0:
                    return False
                else:
                    result.pop()
        if len(result) == 0:
            return True
        else: 
            return False

你可能感兴趣的:(Leecode)