LandscapeMi

landscapemi的博客

leetcode_c++:图:Reconstruct Itinerary (332)


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

class Solution {
public:
    vector<string> findItinerary(vector<pair<string, 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);
    }
};
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mijian1207mijian/article/details/52345918
个人分类: leetcode(c++)
上一篇leetcode_c++:图:Minimum Height Trees(310)
下一篇leetcode_c++:Divide and Conquer:Kth Largest Element in an Array(215)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭