leetcode_c++:图:Reconstruct Itinerary (332)


http://www.cnblogs.com/grandyang/p/5183210.html

class Solution {
public:
    vector<string> findItinerary(vectorstring, string> > tickets) {
        vector<string> res;
        unordered_map<string, multiset<string> > m;
        for (auto a : tickets) {
            m[a.first].insert(a.second);
        }
        dfs(m, "JFK", res);
        return vector<string> (res.rbegin(), res.rend());
    }
    void dfs(unordered_map<string, multiset<string> > &m, string s, vector<string> &res) {
        while (m[s].size()) {
            string t = *m[s].begin();
            m[s].erase(m[s].begin());
            dfs(m, t, res);
        }
        res.push_back(s);
    }
};

你可能感兴趣的:(leetcode(c++))