python数据结构之简单括号匹配(栈的应用)

from Stack import *

def march(lis):
    stack=Stack()
    for i in range(len(lis)):
        if lis[i]=="(":
            stack.push(i)
        else:
            if stack.isempity():
                return '匹配失败'

            else:
                stack.pop()
        if i==len(lis)-1:
            if stack.isempity():
                return "匹配成功"
            else:
                return '匹配失败'

if __name__ == '__main__':
    inp = input()
    lis = list(inp)
    print(march(lis))

猜你喜欢

转载自www.cnblogs.com/ares-python/p/12785064.html