题目:

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:

Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]

Example 2:

Input: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"].
  But it is larger in lexical order.

分析:

给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 出发。

可以将题目理解为一个有向图,飞机票当成图的边,最后求的是结点的顺序,实际上是求一个欧拉回路。

这里我们使用Hierholzer算法求解此问题。

正常来说我们应该先求出度为奇数的点,不过由于这道题告知要从“JFK”开始,所以我们可以直接从JFK开始搜索。

DFS(u):

  while(u存在未被访问过的边e(u, v))

    标记边e(u, v)已被访问

    DFS(v)

  END

  将点u添加到路径集中

还是以上面的为例,从JFK开始,存在未被访问的边(1,3),我们在这个选择3,也就是通往SFO的边(注意此题要求应该是选择字符排序小的点,这里只是模拟一下求解欧拉回路的过程),然后我们将3这条边标记以访问。

然后从SFO开始,存在为被访问的边(4),我们选择4这条边,到达了ATL这个点,同样的4也被标记访问过了。

ATL存在未被访问的边(5,2),我们选择5这条边,到达了SFO这个点,5也被标记访问过。

SFO已经不存在未被访问的边了(4已经被标记访问过了),所以我们将SFO加入到路径集中[SFO],并返回上次访问的点。

此时ATL中还存在2这条边未被访问,我们选择2这条边,到达了JFK这个点,2也标记访问过。

JFK中1还未访问,我们选择1这条边,到达了ATL这个点,注意此时所有的边都已经访问过了,ATL没有边可以继续访问了,我们将ATL加入路径集[SFO,ATL],返回上次访问的点。

此时JFK也没有边访问了,我们将JFK加入[SFO,ATL,JFK]

同理ATL也没有可访问的边了,将ATL加入[SFO,ATL,JFK,ATL]

返回到SFO,也没有边可以访问了,将SFO加入[SFO,ATL,JFK,ATL,SFO]

最后我们回到了出发点JFK,1,3都已被标记访问过,将JFK加入到路径集中得[SFO,ATL,JFK,ATL,SFO,JFK],最后将结果集中数据反转一下即可得到所求得欧拉路径。也就是JFK->SFO->ATL->JFK->ATL->SFO

不过注意由于题中要求字符自然排序最小,所以我们在选择边时,要按照顺序选在下一个访问的结点。例如从JFK开始有通向SFO和ATL两个边,我们选择通往ATL的边,依照这样的规则我们可以得到结果

["JFK","ATL","JFK","SFO","ATL","SFO"]

小技巧:在保存机票起点和终点时,我们可以使用有限队列存储边,优先访问字符小的边。

程序:

C++

class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
for(int i = 0; i < tickets.size(); ++i){
if(map.find(tickets[i][0]) == map.end()){
priority_queue <string, vector<string>, cmp> q;
q.push(tickets[i][1]);
map[tickets[i][0]] = q;
}
else{
map[tickets[i][0]].push(tickets[i][1]);
}
}
findPath("JFK");
reverse(res.begin(), res.end());
return res;
}
void findPath(string begin){
while(map.find(begin) != map.end() && map[begin].size() != 0){
string next = map[begin].top();
map[begin].pop();
findPath(next);
}
res.push_back(begin);
}
private:
struct cmp
{
bool operator() (string a, string b)
{
return a > b;
}
};
vector<string> res;
unordered_map<string, priority_queue <string, vector<string>, cmp>> map;
};

Java

class Solution {
public List<String> findItinerary(List<List<String>> tickets) {
for(List<String> pair:tickets){
String key = pair.get(0);
String value = pair.get(1);
if(!map.containsKey(key)){
PriorityQueue<String> p = new PriorityQueue<>();
p.add(value);
map.put(key, p);
}
else{
map.get(key).add(value);
}
}
getPath("JFK");
return res;
}
private void getPath(String begin){
while(map.containsKey(begin) && map.get(begin).size() != 0){
getPath(map.get(begin).poll());
}
res.add(0, begin);
}
private List<String> res = new ArrayList<>();
private Map<String, PriorityQueue<String>> map = new HashMap<>();
}

LeetCode 332. Reconstruct Itinerary重新安排行程 (C++/Java)的更多相关文章

  1. [leetcode]332. Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  2. 【LeetCode】332. Reconstruct Itinerary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 相似题目 参考资料 日期 题目地址:htt ...

  3. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

  4. 【LeetCode】332. Reconstruct Itinerary

    题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...

  5. 332. Reconstruct Itinerary (leetcode)

    1. build the graph and then dfs -- graph <String, List<String>>,  (the value is sorted a ...

  6. 332 Reconstruct Itinerary 重建行程单

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  7. 332. Reconstruct Itinerary

    class Solution { public: vector<string> path; unordered_map<string, multiset<string>& ...

  8. Java实现 LeetCode 332 重新安排行程

    332. 重新安排行程 给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场 ...

  9. Leetcode 332.重新安排行程

    重新安排行程 给定一个机票的字符串二维数组[from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生 ...

  10. [Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

随机推荐

  1. ArcPy自动绘制大量地图并设置地图要素:Python

      本文介绍基于Python语言中ArcPy模块,实现ArcMap自动批量出图,并对地图要素进行自定义批量设置的方法. 1 任务需求   首先,我们来明确一下本文所需实现的需求.   现有通过Pyth ...

  2. 牛客网-SQL专项训练20

    ①学生.书店和图书三个实体集之间的联系属于:多元联系. 解析: 参与联系的实体集个数大于2个时,为多元联系:这里学生.书店.图书是三个实体,为多元联系. 二元联系指只有两个实体集参与的联系: 自反联系 ...

  3. 基于 PTS 压测轻松玩转问题诊断

    ​简介:性能测试 PTS(Performance Testing Service)是具备强大的分布式压测能力的 SaaS 压测平台,可模拟海量用户的真实业务场景,全方位验证业务站点的性能.容量和稳定性 ...

  4. [K8s] Pod 与容器设计模式 Sidecar

    为什么 Pod 必须是原子调度? 在两个容器紧密协作的场景中,避免调度失败问题. Pod 解决了里面多个容器之间高效共享某些资源和数据,共享网络.共享存储. 容器设计模式? 通过在 Pod 里定义专门 ...

  5. [PHP] Laravel 依赖注入使用不当引起的内存溢出

    业务逻辑: 正常在 controller 方法的参数中注入某个类,方法中使用这个类时发生内存超出提示. 分析: 过往显示,正常使用依赖注入是不存在问题的,那么很有可能是哪里发生了循环引用,导致一直请求 ...

  6. linux 环境下idea 注册过期或激活异常解决

    //@desn:linux 环境下idea 注册过期或激活异常解决 //@desn:码字不宜,转载请注明出处 //@author:张慧源  <turing_zhy@163.com> //@ ...

  7. CF1912K

    题意:给定一个长度为 n 的序列,求有多少个子序列满足该子序列内任意相邻 3 个数的和为偶数. 只与奇偶有关,所以可以把状态转化为 01 的. 设 \(f[i][x][y]\) 表示在前 i 个数中, ...

  8. 手把手教你整Win10的Linux子系统(Ubuntu)

    手把手教你整Win10的Linux子系统(Ubuntu) https://www.bilibili.com/read/cv7770224/ 设置root用户的密码 可以通过 sudo passwd r ...

  9. linux 下jq的使用

    安装: yum install jq -y 文档: https://stedolan.github.io/jq/manual/ 更多: https://blog.csdn.net/Cheat11730 ...

  10. linux 系统tar文件压缩打包命令

    打包成tar.gz格式压缩包 # tar -zcvf renwolesshel.tar.gz /renwolesshel 解压tar.gz格式压缩包 # tar zxvf renwolesshel.t ...