华为OD机考题(HJ77 火车进站)

 前言

经过前期的数据结构和算法学习,开始以OD机考题作为练习题,继续加强下熟练程度。

描述

给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号,火车站只有一个方向进出,同时停靠在火车站的列车中,只有后进站的出站了,先进站的才能出站。

要求输出所有火车出站的方案,以字典序排序输出。

数据范围:1≤𝑛≤10 1≤n≤10 

进阶:时间复杂度:𝑂(𝑛!) O(n!) ,空间复杂度:𝑂(𝑛) O(n) 

输入描述:

第一行输入一个正整数N(0 < N <= 10),第二行包括N个正整数,范围为1到10。

输出描述:

输出以字典序从小到大排序的火车出站序列号,每个编号以空格隔开,每个输出序列换行,具体见sample。

示例1

输入:

3
1 2 3

输出:

1 2 3
1 3 2
2 1 3
2 3 1
3 2 1

说明:

第一种方案:1进、1出、2进、2出、3进、3出
第二种方案:1进、1出、2进、3进、3出、2出
第三种方案:1进、2进、2出、1出、3进、3出
第四种方案:1进、2进、2出、3进、3出、1出
第五种方案:1进、2进、3进、3出、2出、1出
请注意,[3,1,2]这个序列是不可能实现的。 

实现原理与步骤

使用递归和回溯的方法,在每一步选择要么将火车压入栈中,要么从栈中弹出一个火车。直到所有火车都进站并且出站完毕。

  • 输入输出

    • inOrder:火车的进站顺序数组。
    • 输出:所有可能的出站顺序的列表。
  • 主函数

    • 创建一个 result 列表来存储所有可能的出站顺序。
    • 创建一个栈 stack 来模拟火车进站的过程。
    • 创建一个列表 currentOutOrder 来存储当前递归过程中构建的出站顺序。
    • 调用辅助递归函数 getAllPossibleOutOrdersHelper 开始递归处理。
  • 递归辅助函数

    • 基本情况:当所有火车都进站且出站完毕(即 index == inOrder.lengthstack 为空),将当前出站顺序添加到结果列表中。
    • 选择1:将下一辆火车压入栈中,然后递归调用。
    • 选择2:如果栈不为空,则将栈顶火车弹出并加入到当前出站顺序中,然后递归调用。
  • 回溯

    • 每次递归返回时,需要恢复栈和出站顺序的状态,以便进行其他选择。

实现代码

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int N = in.nextInt();
        int[] inorder = new int[N];
        for (int i = 0; i < N; i++) {
            inorders[i] = in.nextInt();
        }
        List<List<Integer>> list = getAllPossibleOutOrders(inorder);
        //构造结果
        List<String> resList=new ArrayList();
        for(int i=0;i<list.size();i++){
            List<Integer> temp=list.get(i);
            String res="";
            for(int j=0;j<temp.size();j++){
                res+=temp.get(j)+" ";
            }
            resList.add(res);
        }
        //字典排序并输出
        Collections.sort(resList);
        for(String str:resList){
            System.out.println(str);
        }

    }

    public static List<List<Integer>> getAllPossibleOutOrders(int[] inOrder) {
        List<List<Integer>> result = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        List<Integer> currentOutOrder = new ArrayList<>();

        getAllPossibleOutOrdersHelper(inOrder, 0, stack, currentOutOrder, result);

        return result;
    }

    private static void getAllPossibleOutOrdersHelper(int[] inOrder, int index,
            Stack<Integer> stack, List<Integer> currentOutOrder,
            List<List<Integer>> result) {
        // Base case: if all trains are processed and stack is empty
        if (index == inOrder.length && stack.isEmpty()) {
            result.add(new ArrayList<>(currentOutOrder));
            return;
        }

        // Option 1: Push the next train into the stack
        if (index < inOrder.length) {
            stack.push(inOrder[index]);
            getAllPossibleOutOrdersHelper(inOrder, index + 1, stack, currentOutOrder,result);
            // 回溯
            stack.pop();  
        }

        // Option 2: Pop the top train from the stack if stack is not empty
        if (!stack.isEmpty()) {
            int top = stack.pop();
            currentOutOrder.add(top);
            getAllPossibleOutOrdersHelper(inOrder, index, stack, currentOutOrder, result);
            // 回溯
            currentOutOrder.remove(currentOutOrder.size() - 1);  
            stack.push(top);  
        }
    }

}

1.QA:

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值