LC 332. Reconstruct Itinerary (Euler Path)

link

class Solution {
public:
    stack<string> stk;
    unordered_map<string,multiset<string>> mp;
    vector<string> findItinerary(vector<vector<string>>& tickets) {
        for(auto& t:tickets) mp[t[0]].insert(t[1]);
        dfs("JFK");
        vector<string> res;
        while(!stk.empty()){
            res.push_back(stk.top());
            stk.pop();
        }
        return res;
    }
    
    void dfs(string u){
        while(mp[u].size()){
            auto it=mp[u].begin();
            string next=*it;
            mp[u].erase(it);
            dfs(next);
        }
        stk.push(u);
    }
};

猜你喜欢

转载自www.cnblogs.com/FEIIEF/p/13208882.html