From 8f982799a45a481e3747eb79bd53878187e6d3a6 Mon Sep 17 00:00:00 2001 From: Chunchi Che Date: Sun, 3 Mar 2024 11:37:17 +0800 Subject: [PATCH] Copy list with random pointer (#176) * add copy list with random pointer * impl copy list with random pointer * fix small * fix small --- leetcode-cc/CopyListWithRandPointer.hpp | 62 +++++++++++++++++++ .../src/solutions/first_missing_positive.rs | 2 +- runtime-cc/src/registration.cc | 8 +++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 leetcode-cc/CopyListWithRandPointer.hpp diff --git a/leetcode-cc/CopyListWithRandPointer.hpp b/leetcode-cc/CopyListWithRandPointer.hpp new file mode 100644 index 0000000..19f1453 --- /dev/null +++ b/leetcode-cc/CopyListWithRandPointer.hpp @@ -0,0 +1,62 @@ +#include "problem.h" +#include "solution.h" +#include "unordered_map" + +using namespace std; + +IMPLEMENT_PROBLEM_CLASS(PCopyListWithRandPointer, 138, DIFFI_MEDIUM, + TOPIC_ALGORITHMS, "Copy List With Rand Pointer", "", + {"Hash Table"}); + +class RandNode { + public: + int val; + RandNode* next; + RandNode* random; + + RandNode(int _val) { + val = _val; + next = NULL; + random = NULL; + } +}; + +class SCopyListWithRandPointer : public ISolution { + public: + size_t problemId() const override { return 138; } + string name() const override { + return ("Solution for " + string("Copy List With Rand Pointer")); + } + string location() const override { return __FILE_NAME__; } + int test() const override { return 0; }; + int benchmark() const override { return 0; } + + private: + RandNode* copyRandomList(RandNode* head) { + auto newHead = new RandNode(0); + auto p1 = head; + auto p2 = newHead; + unordered_map map = {}; + + while (p1) { + auto node = new RandNode(p1->val); + p2->next = node; + p2 = p2->next; + map.insert({p1, p2}); + p1 = p1->next; + } + + p1 = head; + p2 = newHead->next; + + while (p1) { + auto rand = map[p1->random]; + p2->random = rand; + + p1 = p1->next; + p2 = p2->next; + } + + return newHead->next; + } +}; diff --git a/leetcode-rs/src/solutions/first_missing_positive.rs b/leetcode-rs/src/solutions/first_missing_positive.rs index 6c2289a..1543ba1 100644 --- a/leetcode-rs/src/solutions/first_missing_positive.rs +++ b/leetcode-rs/src/solutions/first_missing_positive.rs @@ -21,7 +21,7 @@ impl SolutionImpl { for i in 0..n { let target = i as i32 + 1; - if set.get(&target).is_none() { + if !set.contains(&target) { return target; } } diff --git a/runtime-cc/src/registration.cc b/runtime-cc/src/registration.cc index f85bb06..6a9fe54 100644 --- a/runtime-cc/src/registration.cc +++ b/runtime-cc/src/registration.cc @@ -12,6 +12,7 @@ #include "../leetcode-cc/ConstructBTreePreorderInorder.hpp" #include "../leetcode-cc/ConvertSortedArrayToBST.hpp" #include "../leetcode-cc/ConvertSortedListToBST.hpp" +#include "../leetcode-cc/CopyListWithRandPointer.hpp" #include "../leetcode-cc/DecodeWays.hpp" #include "../leetcode-cc/DetIfTwoStrsAreClose.hpp" #include "../leetcode-cc/DeterIfStringHalvesAreAlike.hpp" @@ -573,5 +574,12 @@ const int registerAll(std::shared_ptr handle) { handle->registerSolution( []() -> ArcSolution { return std::make_shared(); }); + handle->registerProblem([]() -> ArcProblem { + return std::make_shared(); + }); + handle->registerSolution([]() -> ArcSolution { + return std::make_shared(); + }); + return 0; }