Skip to content

Commit 01ab000

Browse files
solves #133 in java
1 parent c5b384c commit 01ab000

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers) | [![Java](assets/java.png)](src/SumRootToLeafNumbers.java) | |
120120
| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions) | [![Java](assets/java.png)](src/SurroundedRegions.java) | |
121121
| 131 | [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning) | [![Java](assets/java.png)](src/PalindromePartitioning.java) | |
122-
| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph) | | |
122+
| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph) | [![Java](assets/java.png)](src/CloneGraph.java) | |
123123
| 134 | [Gas Station](https://leetcode.com/problems/gas-station) | | |
124124
| 136 | [Single Number](https://leetcode.com/problems/single-number) | [![Java](assets/java.png)](src/SingleNumber.java) [![Python](assets/python.png)](python/single_number.py) | |
125125
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii) | | |

Diff for: src/CloneGraph.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// https://leetcode.com/problems/clone-graph
2+
// T: O(n)
3+
// S: O(n)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
public class CloneGraph {
13+
private static final class Node {
14+
public int val;
15+
public List<Node> neighbors;
16+
17+
public Node() {
18+
val = 0;
19+
neighbors = new ArrayList<>();
20+
}
21+
22+
public Node(int val) {
23+
this.val = val;
24+
neighbors = new ArrayList<>();
25+
}
26+
27+
public Node(int val, ArrayList<Node> neighbors) {
28+
this.val = val;
29+
this.neighbors = neighbors;
30+
}
31+
}
32+
33+
public Node cloneGraph(Node node) {
34+
if (node == null) return null;
35+
final Map<Integer, Node> graph = new HashMap<>();
36+
cloneGraph(node, new HashSet<>(), graph);
37+
return graph.get(node.val);
38+
}
39+
40+
private void cloneGraph(Node node, Set<Integer> visited, Map<Integer, Node> newGraph) {
41+
if (node == null) return;
42+
if (visited.contains(node.val)) return;
43+
visited.add(node.val);
44+
45+
Node current = getNode(newGraph, node.val);
46+
for (Node adjacent : node.neighbors) {
47+
Node newNeighbour = getNode(newGraph, adjacent.val);
48+
current.neighbors.add(newNeighbour);
49+
cloneGraph(adjacent, visited, newGraph);
50+
}
51+
}
52+
53+
private Node getNode(Map<Integer, Node> graph, int value) {
54+
if (graph.containsKey(value)) return graph.get(value);
55+
Node node = new Node(value);
56+
graph.put(value, node);
57+
return node;
58+
}
59+
}

Diff for: src/HelloWorld.java

-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import java.util.Map;
2-
3-
import static java.util.Map.entry;
4-
51
public class HelloWorld {
62
public static void main(String[] args) {
7-
Map<String, Object> map = Map.of(
8-
"hello", null,
9-
"", null
10-
);
113
}
124
}

0 commit comments

Comments
 (0)