题解 | 栈和排序

栈和排序

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

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Deque<Integer> stack = new ArrayDeque<>();
        int min = n;
        while(n-->0){
            int temp = in.nextInt();
            //尽量按照由大到小的顺序输出
            stack.push(temp);
            if(temp==min){
                System.out.printf("%d ",stack.pop());
                min--;
            }
        }
        while(!stack.isEmpty()){
            System.out.printf("%d ",stack.pop());
        }
    }
}

全部评论

相关推荐

02-11 14:29
已编辑
字节跳动_QA
Edgestr:这种的写代码最狠了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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