class Solution:
# @return a boolean
def isValid(self, s):
st = []
for x in s:
if x in ('(', '[', '{'):
st.append(x)
elif len(st) > 0:
if x == ')' and st[-1] == '(' or \
x == ']' and st[-1] == '[' or \
x == '}' and st[-1] == '{':
st.pop()
else:
return False
else:
return False
return False if st else True
leetcode 日经贴,python code -Valid Parentheses
本文介绍了一个简单的Python类,用于验证括号(包括圆括号、方括号和花括号)是否正确匹配。通过使用栈数据结构,该方法能够有效地检查输入字符串中的括号是否成对出现并正确闭合。


被折叠的 条评论
为什么被折叠?



