Question:codility Lesson7 Brackets
My Answer:
def soluton(S):
if len(S) % 2 == 1:
return 0
nested = {"]":"[","}":"{",")":"("}
half = ["[","{","("]
stack = []
for ele in S:
if ele in half:
stack.append(ele)
else:
if len(stack) == 0:
return 0
elif nested[ele] != stack.pop():
return 0
if len(stack) == 0:
return 1
else:
return 0

本文介绍了一个用于解决Codility Lesson 7中括号匹配问题的Python函数实现。该函数通过栈来判断括号序列是否正确配对,为括号验证提供了一种高效的方法。
393

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



