Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f4a5008

Browse files
🌀 Day 20
1 parent abb6bde commit f4a5008

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
3. [Repeated DNA Sequences](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3498/) ➡️ [CPP Solution](Week3/findRepeatedDnaSequences.cpp)
3232
4. [Best Time to Buy and Sell Stock IV](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3499/) ➡️ [CPP Solution](Week3/maxProfit.cpp)
3333
5. [Minimum Domino Rotations For Equal Row](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3500/) ➡️ [CPP Solution](Week3/minDominoRotations.cpp)
34+
6. [Clone Graph](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3501/) ➡️ [CPP Solution](Week3/cloneGraph.cpp)
3435

3536

3637
## Week 4 🚧

‎Week3/cloneGraph.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
vector<Node*> neighbors;
7+
8+
Node() {
9+
val = 0;
10+
neighbors = vector<Node*>();
11+
}
12+
13+
Node(int _val) {
14+
val = _val;
15+
neighbors = vector<Node*>();
16+
}
17+
18+
Node(int _val, vector<Node*> _neighbors) {
19+
val = _val;
20+
neighbors = _neighbors;
21+
}
22+
};
23+
*/
24+
25+
class Solution {
26+
private:
27+
Node* cloneGraph(Node* node, unordered_map<Node*, Node*>& m) {
28+
if(node == NULL)
29+
return NULL;
30+
31+
if(m.find(node) != m.end())
32+
return m[node];
33+
34+
Node* root = new Node(node->val);
35+
m[node] = root;
36+
37+
for(Node* ng: node->neighbors)
38+
root->neighbors.push_back(cloneGraph(ng, m));
39+
40+
return root;
41+
}
42+
public:
43+
Node* cloneGraph(Node* node) {
44+
unordered_map<Node*, Node*> m;
45+
return cloneGraph(node, m);
46+
}
47+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /