Login dark

https://leetcode-cn.com/problems/valid-parentheses/

我丑陋的代码

class Solution:
    def isValid(self, s: str) -> bool:
        ret = []
        right = ')}]'
        for character in s:
            if character in '({[':
                ret.append(character)
            else:
                if ret != [] and (ord(ret[len(ret)-1]) + 2 == ord(character) or ord(ret[len(ret)-1]) + 1 == ord(character)):
                    ret = ret[:-1]
                else:
                    return False
        return not ret

大神的代码

class Solution:
    def isValid(self, s):
        while '{}' in s or '()' in s or '[]' in s:
            s = s.replace('{}', '')
            s = s.replace('[]', '')
            s = s.replace('()', '')
        return s == ''