1. 程式人生 > >Leetcode Reconstruct Itinerary

Leetcode Reconstruct Itinerary

題意:給出一個有向圖,求出一條尤拉通路。

思路:求尤拉通路。DFS 和後序遍歷。

class Solution {
public:
    vector<string> findItinerary(vector<pair<string, string>> tickets) {
        if(tickets.size() == 0) return re;
        
        for(int i = 0; i < tickets.size(); ++ i) {
            G[tickets[i].first].insert(tickets[i].second);
        }
        dfs("JFK");
        
        return vector<string>(re.rbegin(), re.rend());
    }
    
private:
    map<string, multiset<string>> G;
    vector<string> re;
    
    void dfs(string source) {
        while(G[source].size() != 0) {
            string nextSource = *G[source].begin();
            G[source].erase(G[source].begin());
            dfs(nextSource);
        }
        re.push_back(source);
    }
};

相關推薦

[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

Leetcode Reconstruct Itinerary

題意:給出一個有向圖,求出一條尤拉通路。 思路:求尤拉通路。DFS 和後序遍歷。 class Solution { public: vector<string> findItinerary(vector<pair<string, string

[LeetCode] 332. Reconstruct Itinerary Java

pair sem add 最小路徑 深度 pub logs who 如果 題目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to

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], reconstruc

[LeetCode] Reconstruct Original Digits from English 從英文中重建數字

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. Note: Input contains

LeetCode332 - Reconstruct Itinerary(重新安排行程 圖轉樹 後序遍歷)

LeetCode332 - Reconstruct Itinerary(重新安排行程 圖轉樹 後序遍歷) 題目連結 題目 解析 由於必須要按照字典序最小的來訪問某個結點的孩子,所以在查詢節點的孩子的map中使用一個優先佇列存放,每次取出來的是字典序最小的;

[Swift]LeetCode332. 重新安排行程 | 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 tick

[Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary

sorted eat ash 示例 matching table open sha filter Given a list of airline tickets represented by pairs of departure and arrival airports [

423. Reconstruct Original Digits from English (leetcode

one pty valid 個數 contain tput form end turn Given a non-empty string containing an out-of-order English representation of digits 0-9, o

Leetcode-423 Reconstruct Original Digits from English(從英文中重建數字)

rac har div sort solution cte col ring class 1 class Solution 2 { 3 public: 4 string originalDigits(string s) 5

leetcode】423. Reconstruct Original Digits from English

struct rom {} etc div RKE ont cal mea 題目如下: Given a non-empty string containing an out-of-order English representation of digits 0-9,

[LeetCode]160.Intersection of Two Linked Lists

col style return tro nod sts diff original you Intersection of Two Linked Lists Write a program to find the node at which the intersectio

[LeetCode] Reshape the Matrix 矩陣重塑

ren ati num 我們 資料 call posit tar led In MATLAB, there is a very useful function called ‘reshape‘, which can reshape a matrix into a ne

leetcode題解 || Roman to Integer問題

leet only top xiv har convert 擁有 pro think problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within

100. Same Tree(LeetCode

write end nodes ons code logs 相等 same tree treenode Given two binary trees, write a function to check if they are equal or not. Two binar

LeetCode Length of Last Word

archive n-1 art fine ive lan ets hello style 1. 題目Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘

leetcode 209. Minimum Size Subarray Sum

urn https scrip subarray () ins -s mar color https://leetcode.com/problems/minimum-size-subarray-sum/#/description 題目很簡單。題意就是求字符串中長度最小的一

[LeetCode][Java] Subsets

sort arrays ++ lee 題意 integer sel duplicate ati 題目: Given a set of distinct integers, nums, return all possible subsets. Note: Ele

Leetcode:search_insert_position

size search ++ ins urn leet 位置 art -s 一、 題目 給定一個數組和要插入數的大小。求插入的位置。 二、 分析 太水,直接掃描。過……. class Solution { public: int search

LeetCode)兩個鏈表的第一個公共節點

struct link note lee mono lengthb borde san length LeetCode上面的題目例如以下: Write a program to find the node at which the intersection of t