-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path332.cpp
More file actions
35 lines (33 loc) · 846 Bytes
/
Copy path332.cpp
File metadata and controls
35 lines (33 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "common.h"
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
sort(tickets.begin(), tickets.end());
map<string, vector<string>> m;
for (auto& item : tickets) {
m[item.front()].emplace_back(item.back(), false);
}
dfs(m, "JFK");
reverse(res.begin(), res.end());
return res;
}
void dfs(map<string, vector<string>>& m, string now) {
auto& v = m[now];
while (!v.empty()) {
auto next = v.front();
v.erase(v.begin());
dfs(m, next);
}
res.push_back(now);
}
vector<string> res;
};
int main() {
Solution s;
vector<vector<string>> t = {{"JFK", "SFO"}, {"JFK", "ATL"}, {"SFO", "ATL"}, {"ATL", "JFK"}, {"ATL", "SFO"}};
auto v = s.findItinerary(t);
for (const auto& item : v) {
cout << item << " ";
}
cout << endl;
}