题解 | 栈和排序
栈和排序
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))))