用python + stack实现括号有效问题

def isValid(self, s):
    Parentheses = []
    count = 0
    Parenthese_dict = {'(' : ')',
                       '[' : ']',
                       '{' : '}'}
    for Parenthese in s:
        if Parenthese in Parenthese_dict:
            Parentheses.append(Parenthese)
            count += 1
        else:
            if count == 0 or Parenthese != Parenthese_dict[Parentheses[count - 1]]:
                return False
            else:
                del Parentheses[count - 1]
                count -= 1
    if count != 0:
        return False
    return True

猜你喜欢

转载自blog.csdn.net/manmanxiaowugun/article/details/79823267