题解 | 栈和排序

栈和排序

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());
        }
    }
}

全部评论

相关推荐

优秀的大熊猫在okr...:多益:此贼,必有同谋,按律,该当连坐!
你不能接受的企业文化有哪...
点赞 评论 收藏
分享
马上就好了:HR看了以为来卧底来了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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