题解 | #火车站出站#

火车进站

http://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

import java.util.*;

public class Main{

static List<String> l = new ArrayList<>(); //储存结果

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    while (in.hasNext()) {
        l.clear(); //静态变量,每次先清空
        int nums = in.nextInt();
        int[] id = new int[nums];
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums; i++) {
            id[i] = in.nextInt();
        }
        trainOut(id, 0, stack, "", 0);
       //对结果集排序
        Collections.sort(l); 
        for (String str : l) {
            System.out.println(str);
        }
    }
    in.close();
}

//i为入栈次数,n为出栈次数,str存储一趟结果

public static void trainOut(int[] id, int i, Stack<Integer> s, String str, int n) {
    if (n == id.length) {
        l.add(str); //如果所有火车均出栈则将当前结果保存
    }

    if (!s.empty()) { //栈非空时出栈
        int temp = s.pop();
        trainOut(id, i, s, str + temp + " ", n + 1);
        s.push(temp); //恢复现场
    }

    if (i < id.length) {
        s.push(id[i]);
        trainOut(id, i + 1, s, str, n);
        s.pop(); //恢复现场

    }
}

}

全部评论
为什么这个递归不需要终止条件?
5 回复 分享
发布于 2022-03-09 16:28
这个不是栈牛逼,这个对递归思路的运用达到了登峰造极之境界。
4 回复 分享
发布于 2023-04-13 21:26 湖北
代码牛!脑瓜子都想秃噜皮了
4 回复 分享
发布于 2022-08-10 12:11
非常优秀的思路和代码,bravo! 给所有代码打了注释,帮助后来的同学理解: public class Main { static List<string> l = new ArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { l.clear(); //static variable has to be cleared each time int nums = in.nextInt(); //number of trains int[] id = new int[nums]; //used to store the incoming sequence of trains Stack<integer> stack = new Stack<>(); //stack acts like the station for (int i =0; i< nums; i++) { id[i] = in.nextInt(); } //fill out the incoming sequence trainOut(id, 0, stack, "", 0); //Sort the outcome Collections.sort(l); for (String str: l) { System.out.println(str); } } in.close(); } public static void trainOut(int[] id, int i, Stack<integer> s, String str, int n) { // i indicates the position we are in the incoming sequence, n indicates the position we are in the outbound sequence. if ( n == id.length) { //It's like what we do in dfs/backtracking, we set up an ending condition //When we reached the end of the sequence, we add this answer to the output l.add(str); } if (!s.empty()) { //If we still have train in our station, we can either let another train in, or we can ride this train out. int temp = s.pop(); //We first try ride this train out. trainOut(id, i, s, str + temp + " ", n+1); //we need to add space to our answer s.push(temp); //By pushing back, we can get our initial condition in this level of dfs back, and try move this train back and see what happens. } if ( i < id.length) { //if we still have incoming trains s.push(id[i]); //let one train in trainOut(id, i+1, s, str, n); //see what we can do based on this condition s.pop(); //after this line we will get back to the previous level of recursion //so we need to restore the stack as they are literally using the same stack at the same address } } }</integer></integer></string>
2 回复 分享
发布于 2022-12-03 14:32 美国
我的思路和你一样,但是我的少了还原那步,一直想不到这一步,真的厉害
点赞 回复 分享
发布于 2023-11-29 22:39 湖北
为什么进站不加stack.pop();出站不加 stack.push(temp);,就只有一种结果3 2 1
点赞 回复 分享
发布于 2023-09-14 16:08 江苏
是不是少了递归的出口
点赞 回复 分享
发布于 2023-07-06 10:24 美国
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static List<string> output = new ArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = in.nextInt(); int[] numbers = new int[count]; for (int i = 0; i < count; i++) { numbers[i] = in.nextInt(); } Stack<integer> stack = new Stack<>(); String curOut = ""; process(numbers,stack,0,curOut); Collections.sort(output); for(String curr:output){ System.out.println(curr); } } /** * stack要么进,要么出。先进一个,处理完这个进的,然后恢复原样;再出,处理完这个出的,就完事了。 * @param numbers 编号数组 * @param stack 核心栈 * @param i 当前编号 * @param curOut 当前已经出栈的数字 */ public static void process(int[] numbers, Stack<integer> stack, int i, String curOut) { //如果出栈数字已经满了,就先添加 if(curOut.length() == numbers.length) { output.add(curOut); } //如果出栈的数字没有满,此时可以出栈也可以进栈,都可以。 //这里先做出栈 //出栈需要建立在栈本身不为空的基础上 if(!stack.isEmpty()) { //出栈 int temp = stack.pop(); //继续后面的动作,一条路走到黑 //出栈,该数组添加到temp process(numbers,stack,i,curOut + temp); //这种出栈的场景走完后,下一个场景,进栈,不过先恢复原样 stack.push(temp); } //开始做入栈场景 //入账时要确保还有数据入账 if (i < numbers.length) { //入账 int temp2 = numbers[i]; stack.push(temp2); //继续后面的操作,一条路走到黑 //i位置移动到下一个 process(numbers,stack,i+ 1,curOut); //这种场景走完后,恢复原样 stack.pop(); } } } 把大佬的代码研究了几天,注释了一下</integer></integer></string>
点赞 回复 分享
发布于 2023-04-15 12:00 湖北
感觉大脑的cpu烧了
点赞 回复 分享
发布于 2023-04-07 15:55 北京
这个栈真的牛逼
点赞 回复 分享
发布于 2023-02-17 23:46 广东
很秀,栈处理的逻辑复杂了就很难理解,看了很久才懂
点赞 回复 分享
发布于 2023-02-05 13:41 广西
调试都跟不上这脑瓜子
点赞 回复 分享
发布于 2022-10-24 20:12 北京
真是天才
点赞 回复 分享
发布于 2022-10-18 21:55 广东
这个聪明
点赞 回复 分享
发布于 2022-10-05 23:28 上海

相关推荐

01-30 09:45
燕山大学 Java
喵_coding:这种直接跑就完事了 哪有毕业了才签合同 任何offer和三方都没有的
点赞 评论 收藏
分享
评论
71
28
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
8542次浏览 77人参与
# 你的实习产出是真实的还是包装的? #
1580次浏览 40人参与
# 巨人网络春招 #
11283次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7320次浏览 40人参与
# 简历第一个项目做什么 #
31468次浏览 323人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186755次浏览 1118人参与
# 米连集团26产品管培生项目 #
5484次浏览 214人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152222次浏览 887人参与
# 研究所笔面经互助 #
118833次浏览 577人参与
# 重来一次,我还会选择这个专业吗 #
433251次浏览 3926人参与
# 简历中的项目经历要怎么写? #
309889次浏览 4181人参与
# 面试紧张时你会有什么表现? #
30463次浏览 188人参与
# 你今年的平均薪资是多少? #
212941次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63209次浏览 791人参与
# 我的求职精神状态 #
447934次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76375次浏览 374人参与
# 正在春招的你,也参与了去年秋招吗? #
363077次浏览 2635人参与
# 你怎么看待AI面试 #
179724次浏览 1223人参与
# 牛客AI文生图 #
21391次浏览 237人参与
# 职能管理面试记录 #
10776次浏览 59人参与
# 网易游戏笔试 #
6445次浏览 83人参与
# 腾讯音乐求职进展汇总 #
160536次浏览 1109人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务