Skip to content

Commit f894fff

Browse files
refactor 133
1 parent 616a7e5 commit f894fff

File tree

2 files changed

+27
-55
lines changed

2 files changed

+27
-55
lines changed

src/main/java/com/fishercoder/common/classes/UndirectedGraphNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Created by fishercoder1534 on 9/30/16.
88
*/
99
public class UndirectedGraphNode {
10-
public int label;
10+
public int val;
1111
public List<UndirectedGraphNode> neighbors;
1212

1313
@Override
@@ -21,21 +21,21 @@ public boolean equals(Object o) {
2121

2222
UndirectedGraphNode that = (UndirectedGraphNode) o;
2323

24-
if (label != that.label) {
24+
if (val != that.val) {
2525
return false;
2626
}
2727
return neighbors != null ? neighbors.equals(that.neighbors) : that.neighbors == null;
2828
}
2929

3030
@Override
3131
public int hashCode() {
32-
int result = label;
32+
int result = val;
3333
result = 31 * result + (neighbors != null ? neighbors.hashCode() : 0);
3434
return result;
3535
}
3636

3737
public UndirectedGraphNode(int x) {
38-
label = x;
38+
val = x;
3939
neighbors = new ArrayList<>();
4040
}
4141
}

src/main/java/com/fishercoder/solutions/_133.java

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,31 @@
77
import java.util.Map;
88
import java.util.Queue;
99

10-
/**
11-
* 133. Clone Graph
12-
13-
Clone an undirected graph.
14-
Each node in the graph contains a label and a list of its neighbors.
15-
16-
17-
OJ's undirected graph serialization:
18-
Nodes are labeled uniquely.
19-
20-
We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
21-
As an example, consider the serialized graph {0,1,2#1,2#2,2}.
22-
23-
The graph has a total of three nodes, and therefore contains three parts as separated by #.
24-
25-
First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
26-
Second node is labeled as 1. Connect node 1 to node 2.
27-
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
28-
Visually, the graph looks like the following:
29-
30-
1
31-
/ \
32-
/ \
33-
0 --- 2
34-
/ \
35-
\_/
36-
37-
*/
3810
public class _133 {
3911

40-
public static class Solution1 {
41-
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
42-
if (node == null) {
43-
return node;
44-
}
45-
46-
Map<Integer, UndirectedGraphNode> map = new HashMap();
47-
Queue<UndirectedGraphNode> queue = new LinkedList();
48-
UndirectedGraphNode root = new UndirectedGraphNode(node.label);
49-
map.put(root.label, root);
50-
queue.offer(node);
51-
//remember to offer the original input node into the queue which contains all the information
52-
while (!queue.isEmpty()) {
53-
UndirectedGraphNode curr = queue.poll();
54-
for (UndirectedGraphNode eachNode : curr.neighbors) {
55-
if (!map.containsKey(eachNode.label)) {
56-
map.put(eachNode.label, new UndirectedGraphNode(eachNode.label));
57-
queue.offer(eachNode);
58-
}
59-
map.get(curr.label).neighbors.add(map.get(eachNode.label));
12+
public static class Solution1 {
13+
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
14+
if (node == null) {
15+
return node;
16+
}
17+
18+
Map<Integer, UndirectedGraphNode> map = new HashMap();
19+
Queue<UndirectedGraphNode> queue = new LinkedList();
20+
UndirectedGraphNode root = new UndirectedGraphNode(node.val);
21+
map.put(root.val, root);
22+
queue.offer(node);
23+
//remember to offer the original input node into the queue which contains all the information
24+
while (!queue.isEmpty()) {
25+
UndirectedGraphNode curr = queue.poll();
26+
for (UndirectedGraphNode eachNode : curr.neighbors) {
27+
if (!map.containsKey(eachNode.val)) {
28+
map.put(eachNode.val, new UndirectedGraphNode(eachNode.val));
29+
queue.offer(eachNode);
30+
}
31+
map.get(curr.val).neighbors.add(map.get(eachNode.val));
32+
}
33+
}
34+
return root;
6035
}
61-
}
62-
return root;
6336
}
64-
}
6537
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy