LeetCode-332. Reconstruct Itinerary
https://leetcode.com/problems/reconstruct-itinerary/
You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". 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"].
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
Example 1:

Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]] Output: ["JFK","MUC","LHR","SFO","SJC"]
Example 2:

Input: tickets = [["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.
Constraints:
1 <= tickets.length <= 300tickets[i].length == 2fromi.length == 3toi.length == 3fromiandtoiconsist of uppercase English letters.fromi != toi
【C++】
1.Reconstruct Itinerary
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
vector<string> ans;
if (tickets.empty()) {return ans;}
unordered_map<string, multiset<string>> hash;
for (const auto & ticket: tickets) {
hash[ticket[0]].insert(ticket[1]);
}
stack<string> s;
s.push("JFK");
while (!s.empty()) {
string next = s.top();
if (hash[next].empty()) {
ans.push_back(next);
s.pop();
} else {
s.push(*hash[next].begin());
hash[next].erase(hash[next].begin());
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
2. DFS without stack
class Solution {
unordered_map<string, multiset<string>> adj;
void dfs(vector<string> &ans, string src) {
while (!adj[src].empty()) {
string dst = *adj[src].begin();
adj[src].erase(adj[src].begin());
dfs(ans, dst);
}
ans.push_back(src);
}
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
vector<string> ans;
if (tickets.empty()) {return ans;}
for (int i = 0; i < tickets.size(); i++) {adj[tickets[i][0]].insert(tickets[i][1]);}
dfs(ans, "JFK");
reverse(ans.begin(), ans.end());
return ans;
}
};
【Java】
class Solution {
Map<String, PriorityQueue<String>> map = new HashMap<>();
void dfs(List<String> result, String start){
PriorityQueue<String> qu = map.getOrDefault(start, new PriorityQueue<>());
while (!qu.isEmpty()) {dfs(result, qu.remove());}
result.add(start);
}
public List<String> findItinerary(List<List<String>> tickets) {
List<String> result = new ArrayList<>();
for(List<String> ticket : tickets){
map.putIfAbsent(ticket.get(0), new PriorityQueue<String>());
map.get(ticket.get(0)).add(ticket.get(1));
}
dfs(result, "JFK");
Collections.reverse(result);
return result;
}
}
本文解析了LeetCode题目332,介绍了如何根据给定的航班票务信息,按最小字典序重构旅行路线。涉及C++和Java解决方案,展示了使用栈和深度优先搜索(DFS)的方法。

789

被折叠的 条评论
为什么被折叠?



