Skip to content

Commit 66c6b09

Browse files
committed
feat: add solutions to lc problem: No.0133
No.0133.Clone Graph
1 parent 31e8593 commit 66c6b09

File tree

6 files changed

+394
-34
lines changed

6 files changed

+394
-34
lines changed

solution/0100-0199/0133.Clone Graph/README.md

+136-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
<li>图是连通图,你可以从给定节点访问到所有节点。</li>
7777
</ol>
7878

79-
8079
## 解法
8180

8281
<!-- 这里可写通用的实现逻辑 -->
@@ -88,15 +87,76 @@
8887
<!-- 这里可写当前语言的特殊实现逻辑 -->
8988

9089
```python
91-
90+
"""
91+
# Definition for a Node.
92+
class Node:
93+
def __init__(self, val = 0, neighbors = None):
94+
self.val = val
95+
self.neighbors = neighbors if neighbors is not None else []
96+
"""
97+
98+
99+
class Solution:
100+
def cloneGraph(self, node: 'Node') -> 'Node':
101+
visited = defaultdict()
102+
103+
def clone(node):
104+
if node is None:
105+
return None
106+
if node in visited:
107+
return visited[node]
108+
c = Node(node.val)
109+
visited[node] = c
110+
for e in node.neighbors:
111+
c.neighbors.append(clone(e))
112+
return c
113+
114+
return clone(node)
92115
```
93116

94117
### **Java**
95118

96119
<!-- 这里可写当前语言的特殊实现逻辑 -->
97120

98121
```java
122+
/*
123+
// Definition for a Node.
124+
class Node {
125+
public int val;
126+
public List<Node> neighbors;
127+
public Node() {
128+
val = 0;
129+
neighbors = new ArrayList<Node>();
130+
}
131+
public Node(int _val) {
132+
val = _val;
133+
neighbors = new ArrayList<Node>();
134+
}
135+
public Node(int _val, ArrayList<Node> _neighbors) {
136+
val = _val;
137+
neighbors = _neighbors;
138+
}
139+
}
140+
*/
141+
142+
class Solution {
143+
private Map<Node, Node> visited = new HashMap<>();
99144

145+
public Node cloneGraph(Node node) {
146+
if (node == null) {
147+
return null;
148+
}
149+
if (visited.containsKey(node)) {
150+
return visited.get(node);
151+
}
152+
Node clone = new Node(node.val);
153+
visited.put(node, clone);
154+
for (Node e : node.neighbors) {
155+
clone.neighbors.add(cloneGraph(e));
156+
}
157+
return clone;
158+
}
159+
}
100160
```
101161

102162
### **TypeScript**
@@ -114,7 +174,7 @@
114174
* }
115175
*/
116176

117-
function cloneGraph(node: Node | null): Node | null {
177+
function cloneGraph(node: Node | null): Node | null {
118178
if (node == null) return null;
119179

120180
const visited = new Map();
@@ -136,6 +196,79 @@ function cloneGraph(node: Node | null): Node | null {
136196
};
137197
```
138198

199+
### **C++**
200+
201+
```cpp
202+
/*
203+
// Definition for a Node.
204+
class Node {
205+
public:
206+
int val;
207+
vector<Node*> neighbors;
208+
Node() {
209+
val = 0;
210+
neighbors = vector<Node*>();
211+
}
212+
Node(int _val) {
213+
val = _val;
214+
neighbors = vector<Node*>();
215+
}
216+
Node(int _val, vector<Node*> _neighbors) {
217+
val = _val;
218+
neighbors = _neighbors;
219+
}
220+
};
221+
*/
222+
223+
class Solution {
224+
public:
225+
unordered_map<Node*, Node*> visited;
226+
227+
Node* cloneGraph(Node* node) {
228+
if (!node) return nullptr;
229+
if (visited.count(node)) return visited[node];
230+
Node* clone = new Node(node->val);
231+
visited[node] = clone;
232+
for (auto& e : node->neighbors)
233+
clone->neighbors.push_back(cloneGraph(e));
234+
return clone;
235+
}
236+
};
237+
```
238+
239+
### **Go**
240+
241+
```go
242+
/**
243+
* Definition for a Node.
244+
* type Node struct {
245+
* Val int
246+
* Neighbors []*Node
247+
* }
248+
*/
249+
250+
func cloneGraph(node *Node) *Node {
251+
visited := map[*Node]*Node{}
252+
var clone func(node *Node) *Node
253+
clone = func(node *Node) *Node {
254+
if node == nil {
255+
return nil
256+
}
257+
if _, ok := visited[node]; ok {
258+
return visited[node]
259+
}
260+
c := &Node{node.Val, []*Node{}}
261+
visited[node] = c
262+
for _, e := range node.Neighbors {
263+
c.Neighbors = append(c.Neighbors, clone(e))
264+
}
265+
return c
266+
}
267+
268+
return clone(node)
269+
}
270+
```
271+
139272
### **...**
140273

141274
```

solution/0100-0199/0133.Clone Graph/README_EN.md

+136-20
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@
66

77
<p>Given a reference of a node in a&nbsp;<strong><a href="https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph" target="_blank">connected</a></strong>&nbsp;undirected graph.</p>
88

9-
10-
119
<p>Return a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank"><strong>deep copy</strong></a> (clone) of the graph.</p>
1210

13-
14-
1511
<p>Each node in the graph contains a val (<code>int</code>) and a list (<code>List[Node]</code>) of its neighbors.</p>
1612

17-
18-
1913
<pre>
2014

2115
class Node {
@@ -28,27 +22,16 @@ class Node {
2822

2923
</pre>
3024

31-
32-
3325
<p>&nbsp;</p>
3426

35-
36-
3727
<p><strong>Test case format:</strong></p>
3828

39-
40-
4129
<p>For simplicity sake, each&nbsp;node&#39;s value is the same as the node&#39;s index (1-indexed). For example, the first node with&nbsp;<code>val = 1</code>, the second node with <code>val = 2</code>, and so on.&nbsp;The graph is represented in the test case using an adjacency list.</p>
4230

43-
44-
4531
<p><b>Adjacency list</b>&nbsp;is a collection of unordered&nbsp;<b>lists</b>&nbsp;used to represent a finite graph. Each&nbsp;list&nbsp;describes the set of neighbors of a node in the graph.</p>
4632

47-
48-
4933
<p>The given node will&nbsp;always be the first node&nbsp;with&nbsp;<code>val = 1</code>. You must return the <strong>copy of the given node</strong> as a reference to the cloned graph.</p>
5034

51-
5235
<p>&nbsp;</p>
5336
<p><strong>Example 1:</strong></p>
5437
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0133.Clone%20Graph/images/133_clone_graph_question.png" style="width: 500px; height: 550px;" />
@@ -96,21 +79,81 @@ class Node {
9679
<li>The Graph is connected and all nodes can be visited starting from the given node.</li>
9780
</ul>
9881

99-
10082
## Solutions
10183

10284
<!-- tabs:start -->
10385

10486
### **Python3**
10587

10688
```python
107-
89+
"""
90+
# Definition for a Node.
91+
class Node:
92+
def __init__(self, val = 0, neighbors = None):
93+
self.val = val
94+
self.neighbors = neighbors if neighbors is not None else []
95+
"""
96+
97+
98+
class Solution:
99+
def cloneGraph(self, node: 'Node') -> 'Node':
100+
visited = defaultdict()
101+
102+
def clone(node):
103+
if node is None:
104+
return None
105+
if node in visited:
106+
return visited[node]
107+
c = Node(node.val)
108+
visited[node] = c
109+
for e in node.neighbors:
110+
c.neighbors.append(clone(e))
111+
return c
112+
113+
return clone(node)
108114
```
109115

110116
### **Java**
111117

112118
```java
119+
/*
120+
// Definition for a Node.
121+
class Node {
122+
public int val;
123+
public List<Node> neighbors;
124+
public Node() {
125+
val = 0;
126+
neighbors = new ArrayList<Node>();
127+
}
128+
public Node(int _val) {
129+
val = _val;
130+
neighbors = new ArrayList<Node>();
131+
}
132+
public Node(int _val, ArrayList<Node> _neighbors) {
133+
val = _val;
134+
neighbors = _neighbors;
135+
}
136+
}
137+
*/
113138

139+
class Solution {
140+
private Map<Node, Node> visited = new HashMap<>();
141+
142+
public Node cloneGraph(Node node) {
143+
if (node == null) {
144+
return null;
145+
}
146+
if (visited.containsKey(node)) {
147+
return visited.get(node);
148+
}
149+
Node clone = new Node(node.val);
150+
visited.put(node, clone);
151+
for (Node e : node.neighbors) {
152+
clone.neighbors.add(cloneGraph(e));
153+
}
154+
return clone;
155+
}
156+
}
114157
```
115158

116159
### **TypeScript**
@@ -128,7 +171,7 @@ class Node {
128171
* }
129172
*/
130173

131-
function cloneGraph(node: Node | null): Node | null {
174+
function cloneGraph(node: Node | null): Node | null {
132175
if (node == null) return null;
133176

134177
const visited = new Map();
@@ -150,6 +193,79 @@ function cloneGraph(node: Node | null): Node | null {
150193
};
151194
```
152195

196+
### **C++**
197+
198+
```cpp
199+
/*
200+
// Definition for a Node.
201+
class Node {
202+
public:
203+
int val;
204+
vector<Node*> neighbors;
205+
Node() {
206+
val = 0;
207+
neighbors = vector<Node*>();
208+
}
209+
Node(int _val) {
210+
val = _val;
211+
neighbors = vector<Node*>();
212+
}
213+
Node(int _val, vector<Node*> _neighbors) {
214+
val = _val;
215+
neighbors = _neighbors;
216+
}
217+
};
218+
*/
219+
220+
class Solution {
221+
public:
222+
unordered_map<Node*, Node*> visited;
223+
224+
Node* cloneGraph(Node* node) {
225+
if (!node) return nullptr;
226+
if (visited.count(node)) return visited[node];
227+
Node* clone = new Node(node->val);
228+
visited[node] = clone;
229+
for (auto& e : node->neighbors)
230+
clone->neighbors.push_back(cloneGraph(e));
231+
return clone;
232+
}
233+
};
234+
```
235+
236+
### **Go**
237+
238+
```go
239+
/**
240+
* Definition for a Node.
241+
* type Node struct {
242+
* Val int
243+
* Neighbors []*Node
244+
* }
245+
*/
246+
247+
func cloneGraph(node *Node) *Node {
248+
visited := map[*Node]*Node{}
249+
var clone func(node *Node) *Node
250+
clone = func(node *Node) *Node {
251+
if node == nil {
252+
return nil
253+
}
254+
if _, ok := visited[node]; ok {
255+
return visited[node]
256+
}
257+
c := &Node{node.Val, []*Node{}}
258+
visited[node] = c
259+
for _, e := range node.Neighbors {
260+
c.Neighbors = append(c.Neighbors, clone(e))
261+
}
262+
return c
263+
}
264+
265+
return clone(node)
266+
}
267+
```
268+
153269
### **...**
154270

155271
```

0 commit comments

Comments
 (0)