leetcode_c++:图:Reconstruct Itinerary (332)

原创 2016年08月28日 15:16:39

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);
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode 332. Reconstruct Itinerary

题目【难度:Medium】:Given a list of airline tickets represented by pairs of departure and arrival airports...

[leetcode] 332. Reconstruct Itinerary 解题报告

题目链接:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represen...

LeetCode 332. Reconstruct Itinerary【medium】

原题: Given a list of airline tickets represented by pairs of departure and arrival airports [fro...
  • o555ddd
  • o555ddd
  • 2016年10月02日 16:42
  • 132

Leetcode 332. Reconstruct Itinerary

332. Reconstruct Itinerary Total Accepted: 19256 Total Submissions: 71899 Difficulty: Medium Giv...

Leetcode——310 Minimum Height Trees && 332 Reconstruct Itinerary

本周记录两道图论的题目,第一道是关于BFS的题目,310, Minimum Height Trees ;第二道是关于DFS的题目,332,Reconstruct Itinerary。 一、Minim...

Leetcode: 332.Reconstruct Itinerary

Leetcode: 332.Reconstruct Itinerary

LeetCode 332 : Reconstruct Itinerary

Reconstruct Itinerary该题是常规的图搜索相关的题目,使用dfs便可以解决,可惜自己还是折腾了好久。 先贴上我原先的代码:#include #include #include ...

LeetCode 332. Reconstruct Itinerary(重构行程)

原题网址:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represen...
  • jmspan
  • jmspan
  • 2016年04月29日 08:06
  • 655

Leetcode 332. Reconstruct Itinerary[medium]

题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, t...

leetcode 332. Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:图:Reconstruct Itinerary (332)
举报原因:
原因补充:

(最多只允许输入30个字)