Movatterモバイル変換


[0]ホーム

URL:


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

Commitf894fff

Browse files
refactor 133
1 parent616a7e5 commitf894fff

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
publicclassUndirectedGraphNode {
10-
publicintlabel;
10+
publicintval;
1111
publicList<UndirectedGraphNode>neighbors;
1212

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

2222
UndirectedGraphNodethat = (UndirectedGraphNode)o;
2323

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

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

3737
publicUndirectedGraphNode(intx) {
38-
label =x;
38+
val =x;
3939
neighbors =newArrayList<>();
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
importjava.util.Map;
88
importjava.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
publicclass_133 {
3911

40-
publicstaticclassSolution1 {
41-
publicUndirectedGraphNodecloneGraph(UndirectedGraphNodenode) {
42-
if (node ==null) {
43-
returnnode;
44-
}
45-
46-
Map<Integer,UndirectedGraphNode>map =newHashMap();
47-
Queue<UndirectedGraphNode>queue =newLinkedList();
48-
UndirectedGraphNoderoot =newUndirectedGraphNode(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-
UndirectedGraphNodecurr =queue.poll();
54-
for (UndirectedGraphNodeeachNode :curr.neighbors) {
55-
if (!map.containsKey(eachNode.label)) {
56-
map.put(eachNode.label,newUndirectedGraphNode(eachNode.label));
57-
queue.offer(eachNode);
58-
}
59-
map.get(curr.label).neighbors.add(map.get(eachNode.label));
12+
publicstaticclassSolution1 {
13+
publicUndirectedGraphNodecloneGraph(UndirectedGraphNodenode) {
14+
if (node ==null) {
15+
returnnode;
16+
}
17+
18+
Map<Integer,UndirectedGraphNode>map =newHashMap();
19+
Queue<UndirectedGraphNode>queue =newLinkedList();
20+
UndirectedGraphNoderoot =newUndirectedGraphNode(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+
UndirectedGraphNodecurr =queue.poll();
26+
for (UndirectedGraphNodeeachNode :curr.neighbors) {
27+
if (!map.containsKey(eachNode.val)) {
28+
map.put(eachNode.val,newUndirectedGraphNode(eachNode.val));
29+
queue.offer(eachNode);
30+
}
31+
map.get(curr.val).neighbors.add(map.get(eachNode.val));
32+
}
33+
}
34+
returnroot;
6035
}
61-
}
62-
returnroot;
6336
}
64-
}
6537
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp