题解 | 栈和排序

栈和排序

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

def find_maxstack(n,stack):
    s = []
    output = []
    maxnumber = n
    for num in stack:
        s.append(num)
        # 栈不为空,且栈顶元素为最大值
        while s and s[-1] == maxnumber:
            output.append(s.pop()) # 出栈,并保存操作
            maxnumber -= 1 # 最大值减一
    # 栈中剩余元素直接输出
    while s:
        output.append(s.pop())
    return output


n = int(input().strip())
stack = list(map(int,input().strip().split(" ")))

print(" ".join(map(str,find_maxstack(n,stack))))

全部评论

相关推荐

投递拓竹科技等公司10个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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