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

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

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);
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务