数据结构--栈的应用

# 括号匹配问题:给一个字符串,其中包含小括号、中括号、大括号,求该字符串中的括号是否匹配。
# 例如:
# ()()[]{}		匹配
# ([{()}])		匹配
# [](		不匹配
# [(])		不匹配

def kuohao_match(exp):
    di={'(':')','[':']','{':'}'}
    stack=[]
    for c in exp:
        if c in {'(','[','{'}:
            stack.append(c)
        else:
            if len(stack)==0:
                return False
            top=stack.pop()
            if di[top] != c:
                return False
    if len(stack)>0:
        return False
    else:
        return True



print(kuohao_match('()[]{([])[]}()'))

  

原文地址:https://www.cnblogs.com/morgana/p/7833710.html