题解 | #火车进站#

火车进站

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

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    const n = parseInt(await readline());
    const list = (await readline()).split(" ").map(Number);
    const res = [];
    const dfs = (list,insert,out) =>{
        if(out.length === n) return res.push(out.join(" "));
        if(insert.length){
            out.push(insert.pop());
            dfs(list,insert,out);
            insert.push(out.pop());//还原
        }
        if(list.length){
            insert.push(list.shift());
            dfs(list,insert,out);
            list.unshift(insert.pop());//还原
        }
    }
    dfs(list,[],[]);
    console.log(res.sort().join("\n"));
}()

全部评论

相关推荐

2025-12-10 14:51
门头沟学院 Java
点赞 评论 收藏
分享
哞客37422655...:嫡系回归,buff叠满!好好干,等你们组明年把你当嫡长继承人的时候再请我们喝奶茶~
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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