题解 | #用递归函数和栈逆序一个栈#

用递归函数和栈逆序一个栈

http://www.nowcoder.com/practice/1de82c89cc0e43e9aa6ee8243f4dbefd

import java.util.*;
public class Main{
    public static void dfs(Stack<Integer> stack){
       if(stack.size() == 0){
           return;
       }
        int count = stack.pop();
        dfs(stack);
        System.out.print(count + " ");
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Stack<Integer> stack = new Stack<>();
        int n = sc.nextInt();
        int i = 0;
        while(i < n){
            int a = sc.nextInt();
            stack.push(a);
            i++;
        }
        dfs(stack);
    }
}
全部评论

相关推荐

做个有文化的流氓:幸遇良师,幸遇好的hr
找工作中的小确幸
点赞 评论 收藏
分享
10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:其实简历是不需要事无巨细的写的,让对方知道你有这段经历就行了,最重要的是面试的时候讲细讲明白
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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