332. Reconstruct Itinerary

class Solution {
public:
    vector<string> path;
    unordered_map<string, multiset<string>> m;
    vector<string> findItinerary(vector<pair<string, string>> tickets) {
        for (auto &p : tickets)
            m[p.first].insert(p.second);
        dfs("JFK");
        reverse(path.begin(), path.end());
        return path;
    }
    void dfs(const string cur) {
        while (!m[cur].empty()) {
            auto peer = m[cur].begin();
            string next = *peer;
            m[cur].erase(peer);
            dfs(next);
        }
        path.push_back(cur);
    }
};

 

posted @ 2018-12-02 07:41  JTechRoad  阅读(90)  评论(0编辑  收藏  举报