Question:codility Lesson7 Nesting
My Answer:
def solution(S):
if len(S) % 2 == 1:
return 0
num = 0
for ele in S:
if ele == "(":
num += 1
else:
num -= 1
if num < 0:
return 0
if num == 0:
return 1
else:
return 0

本文介绍了一个解决 Codility Lesson 7 中 Nesting 问题的方法。通过判断字符串中括号是否正确配对,该算法能有效检测输入字符串的有效性。首先检查字符串长度是否为偶数,然后遍历字符串,利用计数器跟踪括号状态,最终确定括号是否完全匹配。
307

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



