Skip to content

Commit

Permalink
Copy list with random pointer (#176)
Browse files Browse the repository at this point in the history
* add copy list with random pointer

* impl copy list with random pointer

* fix small

* fix small
  • Loading branch information
SKTT1Ryze authored Mar 3, 2024
1 parent 7f08faf commit 8f98279
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
62 changes: 62 additions & 0 deletions leetcode-cc/CopyListWithRandPointer.hpp
Original file line number Diff line number Diff line change
@@ -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<RandNode*, RandNode*> 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;
}
};
2 changes: 1 addition & 1 deletion leetcode-rs/src/solutions/first_missing_positive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
8 changes: 8 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -573,5 +574,12 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SWordBreak>(); });

handle->registerProblem([]() -> ArcProblem {
return std::make_shared<PCopyListWithRandPointer>();
});
handle->registerSolution([]() -> ArcSolution {
return std::make_shared<SCopyListWithRandPointer>();
});

return 0;
}

0 comments on commit 8f98279

Please sign in to comment.