题解 | 验证栈序列

验证栈序列

https://www.nowcoder.com/practice/d3178fe362dd4810b577c77c9e128fc5

q = int(input())


for i in range(q):
    n = int(input())
    push = list(map(int, input().strip().split()))
    pop = list(map(int, input().strip().split()))
    stack = []
    for i in range(n):
        stack.append(push[i])
        while len(stack) > 0 and stack[-1] == pop[0]:
            stack.pop()
            del pop[0]
    if len(stack) == 0:
        print("Yes")
    else:
        print("No")

在我做这个题目的时候,我首先想到的是用一个函数去定义,但后来发现,当写完函数之后,如果用循环去对于每一组判断,这会调用大的内存且浪费时间,本题不需要用函数。不知函数递归会如何?

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务