[LeetCode 332] Reconstruct Itinerary

国密(3)- 预主密钥/主密钥计算和Finished消息的加解密 国密的ECC-SM3-SM4-CBC模式下的预主密钥和主密钥的生成过程介绍,以及Finished消息的加解密介绍 阅读详情

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.

Note:

  1. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
  2. All airports are represented by three capital letters (IATA code).
  3. You may assume all tickets form at least one valid itinerary.

Example 1:
tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"].

Example 2:
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.

Solution:

DFS

We know the start point "JFK" and total itinerary length. Use map to store the tickets connect src and dest. Map<String, List<String>>, the list should be in ascending order. use dfs to go through the map, find the smallest lexical order 


public List<String> findItinerary(String[][] tickets) {
        Map<String, List<String>> itineraryMap = new HashMap<>();
        for(String[] ticket : tickets) {
            List<String> dests = itineraryMap.get(ticket[0]);
            if(dests == null) {
                dests = new ArrayList<>();
                dests.add(ticket[1]);
                itineraryMap.put(ticket[0], dests);
            } else{
                dests.add(ticket[1]);    
            }
        }
        for(List<String> dests : itineraryMap.values()) {
            Collections.sort(dests);
        }
        List<String> res = new ArrayList<>();
        res.add("JFK");
        dfs(res, new ArrayList<String>(), itineraryMap, "JFK", tickets.length);
        return res;
    }
    
    private void dfs(List<String> res, List<String> cur, Map<String, List<String>> itineraryMap, String src, int len) {
        if(res.size() >1) {
            return;
        }
        if(cur.size() ==len) {
            res.addAll(cur);
            return;
        }
        List<String> dests = itineraryMap.get(src);
        if(dests != null && dests.size() >0) {
            for(int i=0;i<dests.size();i++) {
                String des = dests.remove(i);
                cur.add(des);
                dfs(res, cur, itineraryMap, des, len);
                dests.add(i, des);
                cur.remove(cur.size()-1);
            }
        }
    }







libcurl库使用详情、libcurl库的制作 ​ libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传, HTTP基本表单上传,代理,cookies,和用户认证。libcurl的官网 、库下载。这种库使用的时候就像使用wiringPi库一样,编写完代码需要链接这个库,所以要先下载这个库。libcurl的使用(其实和socket编程时一样,都需要一定的步骤): 阅读详情

相关推荐

MinIO vs 华为云OBS:成本、性能与兼容性深度对比(含实测数据)

本文深度对比了开源对象存储MinIO与华为云OBS在成本、性能、兼容性及运维方面的核心差异。通过实测数据与全生命周期成本分析,为技术决策者提供选型依据。文章指出,MinIO在长期成本控制、性能深度优化及S3 API兼容性方面优势显著,而华为云OBS则提供开箱即用的便利性。

kmeans3miner的博客 595

【LeetCode】332. 重新安排行程

暴力回溯到欧拉通路,从无脑到数学!

woodwhale的博客 793

Ubuntu22.04安装搜狗输入法踩坑实录:从依赖报错到完美解决的完整指南

本文详细解析了在Ubuntu 22.04系统上安装搜狗拼音输入法的完整流程,从解决依赖报错到配置fcitx框架,再到最终优化使用体验。针对常见的dpkg安装错误和fcitx冲突问题,提供了实用的解决方案和命令,帮助用户顺利完成安装并享受流畅的中文输入体验。

palm99的专栏 974

332. Reconstruct Itinerary(重建行程)

332. Reconstruct Itinerary(重建行程)Reconstruct Itinerary重建行程 题目链接 题目描述 题目分析 方法一深搜回溯 算法描述 方法二深搜 算法描述 参考代码 方法一深搜回溯 方法二分治算法题目链接https://leetcode.com/problems/reconstruct-itinerary/description/题目描述 Given a l

qq_32142535的博客 789

支付宝H5支付唤醒协议全解析:从alipays://到动态orderSuffix的抓取与拼接

本文深入解析支付宝H5支付唤醒协议,从alipays://协议设计到动态orderSuffix参数的抓取与拼接,详细介绍了支付宝H5支付的底层机制。内容包括协议架构剖析、动态令牌生成与捕获、浏览器兼容性解决方案以及安全机制设计,为开发者提供全面的技术指南和实践建议。

weixin_27972919的博客 3506

基于LUT表对鱼眼图像进行畸变校正

当然也可以进行图像的畸变校正操作,已知相机的焦距为fx/fy,光心为cx/cy,像素大小为p(单位mm/pixel)对于畸变校正结果图中像素点(u,v),其对应的原图中的像素坐标(u',v')的计算流程描述如下。1)假设无畸变相机成像模型的焦距和光心与原相机相同,计算得到无畸变相机成像模型中归一化平面坐标(X,Y,Z),其中X=(u-cx)/fx,Y=(v-cy)/fy,Z=1。这里x'/y'的单位为像素。

On my way 639

332. Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus

zshouyi的博客 307

大模型系列——Ollama简明教程

在这篇文章中,我们将了解如何使用 Ollama、如何在 Ollama 中创建自己的模型 以及如何使用 Ollama 构建聊天机器人。这是深入研究 Ollama 的第一部分,以及我所学到的有关本地 LLM 的知识以及如何将它们用于基于推理的应用程序。在这篇文章中,你将了解——

2401_84052244的博客 1万+

Leetcode题解系列——Reconstruct Itinerary(c++版)

题目链接:332. Reconstruct Itinerary 题目大意:给出一个结点对的集合,结点对中为飞机的起飞点与降落点。要求根据飞机机票的信息来重新构造出旅途的线路,即遍历这个图。 注意点: 题目给出一定会存在一个解,即构造的图一定是连通的。 这是一个有向图,要注意起始与结束地点 与一般的深度搜索不同,这次我们不是要搜索结点的访问次序,而是搜索边的访问次序。这就意味着结点可以重复访问,而...

dick的博客 357

作物生长模型Oryza V3实战1:为什么要研究它

ORYZAV3是水稻生长模拟模型ORYZA2000的最新升级版本(3.5版),作为作物生长模型,它通过数学方法和计算机技术模拟水稻生长发育过程及其与环境条件的相互作用。主要功能包括:模拟不同条件下的水稻生长和产量形成、评估环境胁迫影响、分析土壤碳氮动态以及进行区域尺度模拟。国内应用广泛,科研机构用于气候变化影响评估、灌溉模式比较等研究,并开展参数敏感性分析和应用培训。使用流程分为六个步骤:确定输入参数(气象、土壤、作物三类)、参数校准、设置模拟环境、运行模型、分析结果(输出50个参数)和计算产量(穗干重×收

504

华为od 面试八股文_前端_02_含答案

华为od 面试八股文_前端_02_含答案

MISAYAONE的博客 1998

332-Reconstruct Itinerary

Description Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from ...

fighting! 760

王炸联动!OpenClaw 对接微信 / 企业微信保姆级教程,AI 办公效率翻倍

《OpenClaw对接微信/企业微信全指南》摘要:本文提供开源AI智能体OpenClaw与微信生态的完整对接教程。核心内容包括企业微信配置四步法(获取凭证、修改配置、重启服务、验证对接)和个人微信通过企业微信插件的一键打通方案。对接后支持微信/企微发指令AI秒执行、群聊机器人、文件同步等高效办公场景,并附常见问题排查代码。教程强调配置简单(全程可复制代码)、适用广泛(个人/团队场景),实现沟通与AI协作的无缝衔接。文末包含端口放行等实用命令,助力用户快速实现移动端AI办公。(149字)

2501_93651177的博客 584

leetcode 332 Reconstruct Itinerary

Reconstruct Itinerary

GadyPu的专栏 641

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Leetcode 332. Reconstruct Itinerary

Leetcode 332. Reconstruct Itinerary dfs:   熟悉深度优先搜索的代码结构,回溯+递归 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.ut

独钓寒江雪 578

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Leetcode: 332.Reconstruct Itinerary

Leetcode: 332.Reconstruct Itinerary

qq_16318319的博客 493

[leetcode] 332. Reconstruct Itinerary

Description Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from J...

Learning from the mistakes 306

[LeetCode]332. Reconstruct Itinerary

Reconstruct ltinerary

LadyCaro的博客 439

leetcode——332——Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus,

happyxuma1991的博客 392

[leetcode] 332. Reconstruct Itinerary @ python

原题 Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus,...

闲庭信步 564

[LeetCode] 332. Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary

weixin_39629939的博客 327

LeetCode 332. Reconstruct Itinerary

I struggled a while for looking the best data structure to do this DFS... the following is the best solution I think.... #include #include #include #include #include #include using namespace st

ActiveCoder的博客 451

【leetcode】332. Reconstruct Itinerary

题目: 思路: 有向图的深度优先搜索。tickets里实际上保存的是图的有向边。我用unordered_map保存每个from节点(出发地)到其所有邻接to节点(目的地,用一个链表将所有邻接to节点按照字符串从小到大的顺序串起来)的映射关系。每次深度优先遍历邻接点时,都临时将节点从对应链表中删除掉,继续进行更深度的递归。如果能成功,即所有地点都串起来了(总数为边的数量加1),则ok;如果不成功,则将删除的节点再添加回去,继续尝试下一个邻接点。如此往复,直到成功。 代码实现: 代码搞了好几个小时,真就一

zxc120389574的博客 290

[leetcode] 332. Reconstruct Itinerary 解题报告

题目链接:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order.

小榕流光的专栏 6051

LeetCode: 332. Reconstruct Itinerary

LeetCode: 332. Reconstruct Itinerary 题目描述 Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belo...

杨领well的专栏 350

LeetCode //C - 332. Reconstruct Itinerary

【代码】LeetCode //C - 332. Reconstruct Itinerary。

Made in Code 838
上一篇: [LeetCode 335] Self Crossing
下一篇: [LeetCode 330] Patching array
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值