From d90bdb69610d5b5dfa64ba2ea5d44ef8d6bf2b2c Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Mon, 9 Oct 2023 13:45:00 -0400 Subject: [PATCH 1/4] feat: add cpp solution to lc problem: No.0332 --- .../0332.Reconstruct Itinerary/README.md | 37 ++++++++++++++++++ .../0332.Reconstruct Itinerary/README_EN.md | 38 +++++++++++++++++++ .../0332.Reconstruct Itinerary/Solution.cpp | 33 ++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index bd47fa1c4dca8..21a15d06f5eee 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -65,7 +65,44 @@ ```java +``` +### **C++** + +```cpp +class Solution { +public: + std::vector findItinerary(std::vector> &tickets) { + std::unordered_map, std::greater>> g; + std::vector 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(std::unordered_map, std::greater>> &g, std::vector &ret, std::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); + } + } +}; ``` ### **...** diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index ad2e9866078d9..bbceabf218ee5 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -58,6 +58,44 @@ ``` +### **C++** + +```cpp +class Solution { +public: + std::vector findItinerary(std::vector> &tickets) { + std::unordered_map, std::greater>> g; + std::vector 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(std::unordered_map, std::greater>> &g, std::vector &ret, std::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); + } + } +}; +``` + ### **...** ``` diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp new file mode 100644 index 0000000000000..39b4ea09385b4 --- /dev/null +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + std::vector findItinerary(std::vector> &tickets) { + std::unordered_map, std::greater>> g; + std::vector 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(std::unordered_map, std::greater>> &g, std::vector &ret, std::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); + } + } +}; \ No newline at end of file From 38ea7b4978d3bfef060d846fcd2034b2f9c6291a Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Mon, 9 Oct 2023 13:50:22 -0400 Subject: [PATCH 2/4] fix format --- solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp index 39b4ea09385b4..11d48c24c137c 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp @@ -1,11 +1,11 @@ class Solution { public: - std::vector findItinerary(std::vector> &tickets) { + std::vector findItinerary(std::vector>& tickets) { std::unordered_map, std::greater>> g; std::vector ret; // Initialize the graph - for (const auto &t : tickets) { + for (const auto& t : tickets) { g[t[0]].push(t[1]); } @@ -16,7 +16,7 @@ class Solution { return ret; } - void findItineraryInner(std::unordered_map, std::greater>> &g, std::vector &ret, std::string cur) { + void findItineraryInner(std::unordered_map, std::greater>>& g, std::vector& ret, std::string cur) { if (g.count(cur) == 0) { // This is the end point ret.push_back(cur); From 78245a3ed6de24b7140830796771eb83248df0f3 Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Mon, 9 Oct 2023 21:36:48 -0400 Subject: [PATCH 3/4] feat: add cpp solution to lc problem: No.0332 --- .../0300-0399/0332.Reconstruct Itinerary/Solution.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp index 11d48c24c137c..c9a7344be9fee 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.cpp @@ -1,8 +1,8 @@ class Solution { public: - std::vector findItinerary(std::vector>& tickets) { - std::unordered_map, std::greater>> g; - std::vector ret; + vector findItinerary(vector>& tickets) { + unordered_map, greater>> g; + vector ret; // Initialize the graph for (const auto& t : tickets) { @@ -16,7 +16,7 @@ class Solution { return ret; } - void findItineraryInner(std::unordered_map, std::greater>>& g, std::vector& ret, std::string cur) { + void findItineraryInner(unordered_map, greater>>& g, vector& ret, string cur) { if (g.count(cur) == 0) { // This is the end point ret.push_back(cur); From 2a8491607e8f19be8a14469c8fea0b0b19b004ec Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Mon, 9 Oct 2023 21:37:23 -0400 Subject: [PATCH 4/4] feat: add cpp solution to lc problem: No.0332 --- .../0300-0399/0332.Reconstruct Itinerary/README.md | 10 +++++----- .../0300-0399/0332.Reconstruct Itinerary/README_EN.md | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index 1a84cced4c201..10906a75fd998 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -103,12 +103,12 @@ class Solution { ```cpp class Solution { public: - std::vector findItinerary(std::vector> &tickets) { - std::unordered_map, std::greater>> g; - std::vector ret; + vector findItinerary(vector>& tickets) { + unordered_map, greater>> g; + vector ret; // Initialize the graph - for (const auto &t : tickets) { + for (const auto& t : tickets) { g[t[0]].push(t[1]); } @@ -119,7 +119,7 @@ public: return ret; } - void findItineraryInner(std::unordered_map, std::greater>> &g, std::vector &ret, std::string cur) { + void findItineraryInner(unordered_map, greater>>& g, vector& ret, string cur) { if (g.count(cur) == 0) { // This is the end point ret.push_back(cur); diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index 67901769f4a1f..99c61037d1859 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -93,12 +93,12 @@ class Solution { ```cpp class Solution { public: - std::vector findItinerary(std::vector> &tickets) { - std::unordered_map, std::greater>> g; - std::vector ret; + vector findItinerary(vector>& tickets) { + unordered_map, greater>> g; + vector ret; // Initialize the graph - for (const auto &t : tickets) { + for (const auto& t : tickets) { g[t[0]].push(t[1]); } @@ -109,7 +109,7 @@ public: return ret; } - void findItineraryInner(std::unordered_map, std::greater>> &g, std::vector &ret, std::string cur) { + void findItineraryInner(unordered_map, greater>>& g, vector& ret, string cur) { if (g.count(cur) == 0) { // This is the end point ret.push_back(cur);