Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cpp solution to lc problem: No.0332 #1776

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions solution/0300-0399/0332.Reconstruct Itinerary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,44 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> g;
vector<string> ret;

// Initialize the graph
for (const auto& t : tickets) {
g[t[0]].push(t[1]);
}

findItineraryInner(g, ret, "JFK");

ret = {ret.rbegin(), ret.rend()};

return ret;
}

void findItineraryInner(unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& g, vector<string>& ret, string cur) {
if (g.count(cur) == 0) {
// This is the end point
ret.push_back(cur);
return;
} else {
while (!g[cur].empty()) {
auto front = g[cur].top();
g[cur].pop();
findItineraryInner(g, ret, front);
}
ret.push_back(cur);
}
}
};
```

### **...**

```
Expand Down
38 changes: 38 additions & 0 deletions solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> g;
vector<string> ret;

// Initialize the graph
for (const auto& t : tickets) {
g[t[0]].push(t[1]);
}

findItineraryInner(g, ret, "JFK");

ret = {ret.rbegin(), ret.rend()};

return ret;
}

void findItineraryInner(unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& g, vector<string>& ret, string cur) {
if (g.count(cur) == 0) {
// This is the end point
ret.push_back(cur);
return;
} else {
while (!g[cur].empty()) {
auto front = g[cur].top();
g[cur].pop();
findItineraryInner(g, ret, front);
}
ret.push_back(cur);
}
}
};
```

### **...**

```
Expand Down
33 changes: 33 additions & 0 deletions solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> g;
vector<string> ret;

// Initialize the graph
for (const auto& t : tickets) {
g[t[0]].push(t[1]);
}

findItineraryInner(g, ret, "JFK");

ret = {ret.rbegin(), ret.rend()};

return ret;
}

void findItineraryInner(unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& g, vector<string>& ret, string cur) {
if (g.count(cur) == 0) {
// This is the end point
ret.push_back(cur);
return;
} else {
while (!g[cur].empty()) {
auto front = g[cur].top();
g[cur].pop();
findItineraryInner(g, ret, front);
}
ret.push_back(cur);
}
}
};