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

Commit34f62ea

Browse files
author
applewjg
committed
Clone Graph
Change-Id: If70cd55492a20f1da193454f580ddfdb3d41126a
1 parentf4a5127 commit34f62ea

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

‎CloneGraph.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Jan 13, 2015
4+
Problem: Clone Graph
5+
Difficulty: Medium
6+
Source: http://oj.leetcode.com/problems/clone-graph/
7+
Notes:
8+
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
9+
10+
OJ's undirected graph serialization:
11+
Nodes are labeled from 0 to N - 1, where N is the total nodes in the graph.
12+
We use # as a separator for each node, and , as a separator for each neighbor of the node.
13+
As an example, consider the serialized graph {1,2#2#2}.
14+
The graph has a total of three nodes, and therefore contains three parts as separated by #.
15+
Connect node 0 to both nodes 1 and 2.
16+
Connect node 1 to node 2.
17+
Connect node 2 to node 2 (itself), thus forming a self-cycle.
18+
Visually, the graph looks like the following:
19+
20+
1
21+
/ \
22+
/ \
23+
0 --- 2
24+
/ \
25+
\_/
26+
27+
Solution: 1. DFS. 2. BFS.
28+
*/
29+
30+
/**
31+
* Definition for undirected graph.
32+
* class UndirectedGraphNode {
33+
* int label;
34+
* List<UndirectedGraphNode> neighbors;
35+
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
36+
* };
37+
*/
38+
publicclassSolution {
39+
publicUndirectedGraphNodecloneGraph_1(UndirectedGraphNodenode) {
40+
HashMap<UndirectedGraphNode,UndirectedGraphNode>map =newHashMap<UndirectedGraphNode,UndirectedGraphNode>();
41+
returncloneGraphRe(node,map);
42+
}
43+
publicUndirectedGraphNodecloneGraphRe(UndirectedGraphNodenode,HashMap<UndirectedGraphNode,UndirectedGraphNode>map) {
44+
if (node ==null)returnnull;
45+
if (map.containsKey(node) ==true) {
46+
returnmap.get(node);
47+
}
48+
UndirectedGraphNodenewnode =newUndirectedGraphNode(node.label);
49+
map.put(node,newnode);
50+
for (UndirectedGraphNodecur :node.neighbors) {
51+
newnode.neighbors.add(cloneGraphRe(cur,map));
52+
}
53+
returnnewnode;
54+
}
55+
publicUndirectedGraphNodecloneGraph(UndirectedGraphNodenode) {
56+
HashMap<UndirectedGraphNode,UndirectedGraphNode>map =newHashMap<UndirectedGraphNode,UndirectedGraphNode>();
57+
Queue<UndirectedGraphNode>queue =newLinkedList<UndirectedGraphNode>();
58+
if (node ==null)returnnull;
59+
queue.offer(node);
60+
map.put(node,newUndirectedGraphNode(node.label));
61+
while (queue.isEmpty() ==false) {
62+
UndirectedGraphNodecur =queue.poll();
63+
for (UndirectedGraphNodeneighbor :cur.neighbors) {
64+
if (map.containsKey(neighbor) ==false) {
65+
UndirectedGraphNodenewnode =newUndirectedGraphNode(neighbor.label);
66+
map.put(neighbor,newnode);
67+
queue.offer(neighbor);
68+
}
69+
map.get(cur).neighbors.add(map.get(neighbor));
70+
}
71+
}
72+
returnmap.get(node);
73+
}
74+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp