1. 程式人生 > >LeetCode:332. Reconstruct Itinerary(Week 5)

LeetCode:332. Reconstruct Itinerary(Week 5)

332. 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 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.
    
  • 解題思路

    • 本題是關於圖的邊進行遍歷,每張機票都是圖的一條有向邊,需要找出經過每條邊的路徑,並且必定有解本題,則對於某個節點(非起點)其只於一個節點相鄰且只存在一條邊,則這個節點必定是最後訪問的,否則不可能遍歷完所有邊,並且這種點最多一個(不包含起點)。

    • 解法 1 – DFS + 遞迴

      • 解決步驟
        • 將圖建立起來,建立鄰接表,使用map<string, multiset<string> 來儲存鄰接表。使用multiset可以自動排序。(set的預設排序由小到大,multiset預設排序是由大到小
        • 從節點JKF開始DFS遍歷,只要當前的對映集合multiset裡面還有節點,則取出這個節點,遞迴遍歷這個節點,同時需要將這個節點從multiset中刪除掉,當對映集合multiset為空的時候,則將節點加入到結果中
        • 因為當前儲存結果是回溯得到的,需要將結果的儲存順序反轉輸出
      • 實現程式碼
      class Solution {
      public:
          vector<string> findItinerary(vector<pair<string, string>> tickets) {
              vector<string> v;
              map<string, multiset<string> > myMap;
              for(auto it : tickets)
              	myMap[it.first].insert(it.second);
      
              dfs("JFK", v, myMap);
              reverse(v.begin(), v.end());
              return v;
          }
      
          void dfs(string start, vector<string>& v, map<string, multiset<string> > &myMap) {
          	while(myMap[start].size() > 0) {
          		string next = *myMap[start].begin();
          		myMap[start].erase(myMap[start].begin());
          		dfs(next, v, myMap);
          	}
          	v.push_back(start);
          }
      };
      
    • 解法 2 – DFS + 迭代

      • 思路與解法一相同,利用資料結構stack進行迭代。
      • 實現程式碼
      class Solution {
      public:
          vector<string> findItinerary(vector<pair<string, string>> tickets) {
              vector<string> v;
              map<string, multiset<string> > myMap;
              for(auto it : tickets)
              	myMap[it.first].insert(it.second);
      
              stack<string> myStack;
              myStack.push("JFK");
              while(!myStack.empty()) {
              	string node = myStack.top();
              	
              	if(!myMap[node].size()) {
              		myStack.pop();
              		v.push_back(node);
              	} 
              	else {
              		myStack.push(*myMap[node].begin());
              		myMap[node].erase(myMap[node].begin());
              	}
              }
              reverse(v.begin(), v.end());
              return v;
          }
      };