标签:入栈 str object pen stack 返回 元素 条件 style
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack = [] #存放符号 d = {"]": "[", ")":"(", "}":"{"}
“”“
判断符号是否是结束符号,如果是,则判断栈是否是空栈或者栈顶元素是否匹配,如果两者条件满足其一,则返回False
如果不是结束符号,则元素加入栈顶
”“”
for i in s: if i in d.keys(): if stack == [] or d[i] != stack.pop(): return False else: stack.append(i) return stack == []
标签:入栈 str object pen stack 返回 元素 条件 style
原文地址:https://www.cnblogs.com/boluo007/p/12294815.html