首页 > 其他 > 详细

火车进站,华为

时间:2020-07-06 21:29:20      阅读:43      评论:0      收藏:0      [点我收藏+]
import java.util.*;
public class Main {
    static List<String> res;
    static Stack<Integer> stk;
    static void dfs(int[] a, int n, int u, String path, int times) {
        if(times == n) {
            res.add(path);
            return;
        }
        if(!stk.isEmpty()) { // 出站
            int t = stk.pop();
            dfs(a, n, u, path + t + " ", times+1);
            stk.push(t);
        }
        if(n == u) return;
        stk.push(a[u]);
        dfs(a, n, u+1, path, times);
        stk.pop();
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            int[] a = new int[n];
            for(int i=0; i < n; i++) 
                a[i] = sc.nextInt();
            res = new ArrayList<>();
            stk = new Stack<>();
            dfs(a, n, 0, "", 0);
            Collections.sort(res);
            for(int i=0; i < res.size(); i++) {
                System.out.println(res.get(i));
            }
        }
    }
}

火车进站,华为

原文:https://www.cnblogs.com/lixyuan/p/13256481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!