题解 | 验证栈序列

验证栈序列

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

N = int(input())
for _ in range(N):
    n = int(input())
    in_list = list(map(int,input().split()))
    res_list = list(map(int,input().split()))
    stack = []
    out = []
    j = 0
    for i in in_list:
        stack.append(i)
        while stack:
            if stack[-1] == res_list[j]:
                out.append(stack.pop())
                j += 1
            else:
                break
    if out == res_list:
        print("Yes")
    else:
        print("No")

全部评论

相关推荐

评论
2
收藏
分享

创作者周榜

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