题目:重建行程单
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:
- 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"]
. - All airports are represented by three capital letters (IATA code).
- 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.
题意:
给定一个由出发机场和到达机场对表示的飞机票集合列表,按一定顺序重建行程。所有的机票属于一个需要离开JFK的人,所以行程必须从JFK开始。
Note:
1、如果有多个有效的行程,应该返回一个当读成单个字符串时字母顺序小的行程。例如,["JFK","LGA"]行程单的词法顺序要小于["JFK","LGB"]行程单。
2、所有的机场都是由三个大写字母表示的(IATA代码)。
3、可以假定所有机票行程至少有一个是有效的。
转载:http://www.cnblogs.com/grandyang/p/5183210.html
思路一:
这道题给我们一堆飞机票,让我们建立一个行程单,如果有多种方法,取其中字母顺序小的那种方法。这道题的本质是有向图的遍历问题,那么LeetCode关于有向图的题只有两道Course Schedule和Course Schedule II,而那两道是关于有向图的顶点的遍历的,而本题是每张机关于有向图的边的遍历。票都是有向图的一条边,我们需要找出一条经过所有边的路径,那么DFS不是我们的不二选择。先来看递归的结果,我们首先把图建立起来,通过邻接链表来建立。由于题目要求解法按字母顺序小的,那么我们考虑用multiset,可以自动排序。等我们图建立好了以后,从节点JFK开始遍历,只要当前节点映射的multiset里有节点,我们取出这个节点,将其在multiset里删掉,然后继续递归遍历这个节点,由于题目中限定了一定会有解,那么等图中所有的multiset中都没有节点的时候,我们把当前节点存入结果中,然后再一层层回溯回去,将当前节点都存入结果,那么最后我们结果中存的顺序和我们需要的相反的,我们最后再翻转一下即可。
代码:C++版:36ms
class Solution { public: vector<string> findItinerary(vector<pair<string, string>> tickets) { vector<string> res; unordered_map<string, multiset<string>> m; for (auto a : tickets) { m[a.first].insert(a.second); } dfs(m, "JFK", res); return vector<string>(res.rbegin(), res.rend()); } void dfs(unordered_map<string, multiset<string>> &m, string s, vector<string> &res) { while (m[s].size()) { string t = *m[s].begin(); //取出字母顺序小的 m[s].erase(m[s].begin()); //删除取出之后的行程 dfs(m, t, res); } res.push_back(s); } };
思路二:
先构建图,之后再做递归遍历的时候取出遍历到的边,删除已经遍历过得边。与思路一类似。
代码:C++版:31ms
class Solution { unordered_map<string, priority_queue<string, vector<string>, greater<string>>> graph; vector<string> res; void dfs(string vtex) { auto &edges = graph[vtex]; while (!edges.empty()) { string to_vtex = edges.top(); edges.pop(); dfs(to_vtex); } res.push_back(vtex); } public: vector<string> findItinerary(vector<pair<string, string>> tickets) { for (auto e : tickets) graph[e.first].push(e.second); dfs("JFK"); reverse(res.begin(), res.end()); return res; } };
思路三:
迭代的解法,需要借助栈来实现,来实现回溯功能。比如对下面这个例子:
tickets = [["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]]
那么建立的图如下:
JFK -> KUL, NRT
NRT -> JFK
由于multiset是按顺序存的,所有KUL会在NRT之前,那么我们起始从JFK开始遍历,先到KUL,但是KUL没有下家了,这时候图中的边并没有遍历完,此时我们需要将KUL存入栈中,然后继续往下遍历,最后再把栈里的节点存回结果即可。
代码:C++版:36ms
class Solution { public: vector<string> findItinerary(vector<pair<string, string>> tickets) { vector<string> res; stack<string> d; unordered_map<string, multiset<string>> m; for (auto a : tickets) { //将tickets转换存到map中 m[a.first].insert(a.second); } string cur = "JFK"; for (int i=0; i<tickets.size(); ++i) { while (m.find(cur) == m.end() || m[cur].empty()) { d.push(cur); cur = res.back(); res.pop_back(); } res.push_back(cur); string t = cur; cur = *m[cur].begin(); m[t].erase(m[t].begin()); } res.push_back(cur); while (!d.empty()) { res.push_back(d.top()); d.pop(); } return res; } };