[LeetCode] Reconstruct Itinerary 重建行程单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 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, 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 may 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.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

 

以上是关于[LeetCode] Reconstruct Itinerary 重建行程单的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode: Reconstruct Itinerary

[LeetCode] Reconstruct Itinerary 重建行程单

[LeetCode] Reconstruct Itinerary

Leetcode 332: Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary Java