您的位置:首页 > 编程语言 > C语言/C++

leetcode_c++:图:Reconstruct Itinerary (332)

2016-08-28 15:16 525 查看
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);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: