codility Nesting

来源:互联网 发布:sql 添加默认值 编辑:程序博客网 时间:2024/06/06 14:26

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