Skip to content

Commit 04e47e9

Browse files
authored
feat: add cpp solution to lc problem: No.0332 (#1776)
1 parent c4674c0 commit 04e47e9

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

solution/0300-0399/0332.Reconstruct Itinerary/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,44 @@ class Solution {
9898
}
9999
```
100100

101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
vector<string> findItinerary(vector<vector<string>>& tickets) {
107+
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> g;
108+
vector<string> ret;
109+
110+
// Initialize the graph
111+
for (const auto& t : tickets) {
112+
g[t[0]].push(t[1]);
113+
}
114+
115+
findItineraryInner(g, ret, "JFK");
116+
117+
ret = {ret.rbegin(), ret.rend()};
118+
119+
return ret;
120+
}
121+
122+
void findItineraryInner(unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& g, vector<string>& ret, string cur) {
123+
if (g.count(cur) == 0) {
124+
// This is the end point
125+
ret.push_back(cur);
126+
return;
127+
} else {
128+
while (!g[cur].empty()) {
129+
auto front = g[cur].top();
130+
g[cur].pop();
131+
findItineraryInner(g, ret, front);
132+
}
133+
ret.push_back(cur);
134+
}
135+
}
136+
};
137+
```
138+
101139
### **...**
102140
103141
```

solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,44 @@ class Solution {
8888
}
8989
```
9090

91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
vector<string> findItinerary(vector<vector<string>>& tickets) {
97+
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> g;
98+
vector<string> ret;
99+
100+
// Initialize the graph
101+
for (const auto& t : tickets) {
102+
g[t[0]].push(t[1]);
103+
}
104+
105+
findItineraryInner(g, ret, "JFK");
106+
107+
ret = {ret.rbegin(), ret.rend()};
108+
109+
return ret;
110+
}
111+
112+
void findItineraryInner(unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& g, vector<string>& ret, string cur) {
113+
if (g.count(cur) == 0) {
114+
// This is the end point
115+
ret.push_back(cur);
116+
return;
117+
} else {
118+
while (!g[cur].empty()) {
119+
auto front = g[cur].top();
120+
g[cur].pop();
121+
findItineraryInner(g, ret, front);
122+
}
123+
ret.push_back(cur);
124+
}
125+
}
126+
};
127+
```
128+
91129
### **...**
92130
93131
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
vector<string> findItinerary(vector<vector<string>>& tickets) {
4+
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> g;
5+
vector<string> ret;
6+
7+
// Initialize the graph
8+
for (const auto& t : tickets) {
9+
g[t[0]].push(t[1]);
10+
}
11+
12+
findItineraryInner(g, ret, "JFK");
13+
14+
ret = {ret.rbegin(), ret.rend()};
15+
16+
return ret;
17+
}
18+
19+
void findItineraryInner(unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& g, vector<string>& ret, string cur) {
20+
if (g.count(cur) == 0) {
21+
// This is the end point
22+
ret.push_back(cur);
23+
return;
24+
} else {
25+
while (!g[cur].empty()) {
26+
auto front = g[cur].top();
27+
g[cur].pop();
28+
findItineraryInner(g, ret, front);
29+
}
30+
ret.push_back(cur);
31+
}
32+
}
33+
};

0 commit comments

Comments
 (0)