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.
1.当时我的想法是一条道走到底,然后不断加入vector中,但最后出了个bug,就是出现
A-> B
A -> C-> E->A
时,若选择字母排序最前面的,则走到B 就结束了。因此参考了别人的代码,用栈先将每个元素保存起来,然后判断该元素出度的集合是否为0,为0则加入vector中;此时按照上面例子,先将B加入vector中,此时判断A.size>0,则再把C加入stack中,直到A,然后将的size为0,再把A加入vector中;最后将vector颠倒一下输出,即为结果。
下面是这个思想的迭代算法:
vector<string> findItinerary(vector<pair<string, string>> tickets) {
// Each node (airport) contains a set of outgoing edges (destination).
unordered_map<string, multiset<string>> graph;
// We are always appending the deepest node to the itinerary,
// so will need to reverse the itinerary in the end.
vector<string> itinerary;
if (tickets.size() == 0){
return itinerary;
}
// Construct the node and assign outgoing edges
for (pair<string, string> eachTicket : tickets){
graph[eachTicket.first].insert(eachTicket.second);
}
stack<string> dfs;
dfs.push("JFK");
while (!dfs.empty()){
string topAirport = dfs.top();
if (graph[topAirport].empty()){
// If there is no more outgoing edges, append to itinerary
// Two cases:
// 1. If it searchs the terminal end first, it will simply get
// added to the itinerary first as it should, and the proper route
// will still be traversed since its entry is still on the stack.
// 2. If it search the proper route first, the dead end route will also
// get added to the itinerary first.
itinerary.push_back(topAirport);
dfs.pop();
}
else {
// Otherwise push the outgoing edge to the dfs stack and
// remove it from the node.
dfs.push(*(graph[topAirport].begin()));
graph[topAirport].erase(graph[topAirport].begin());
}
}
// Reverse the itinerary.
reverse(itinerary.begin(), itinerary.end());
return itinerary;
}
};
2.递归思想
vector<string> findItinerary(vector<pair<string, string>> tickets) {
for (auto ticket : tickets)
targets[ticket.first].insert(ticket.second);
visit("JFK");
return vector<string>(route.rbegin(), route.rend());
}
map<string, multiset<string>> targets;
vector<string> route;
void visit(string airport) {
while (targets[airport].size()) {
string next = *targets[airport].begin();
targets[airport].erase(targets[airport].begin());
visit(next);
}
route.push_back(airport);
}