★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ubtbxmpg-me.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.node
Nodes are labeled uniquely.git
We use#
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}
.github
The graph has a total of three nodes, and therefore contains three parts as separated by #
.微信
0
. Connect node 0
to both nodes 1
and 2
.1
. Connect node 1
to node 2
.2
. Connect node 2
to node 2
(itself), thus forming a self-cycle. Visually, the graph looks like the following:ide
1
/ \
/ \
0 --- 2
/ \
\_/
克隆一张无向图,图中的每一个节点包含一个 label
(标签)和一个 neighbors
(邻接点)列表 。spa
OJ的无向图序列化:code
节点被惟一标记。orm
咱们用 #
做为每一个节点的分隔符,用 ,
做为节点标签和邻接点的分隔符。htm
例如,序列化无向图 {0,1,2#1,2#2,2}
。
该图总共有三个节点, 被两个分隔符 #
分为三部分。
0
,存在从节点 0
到节点 1
和节点 2
的两条边。1
,存在从节点 1
到节点 2
的一条边。2
,存在从节点 2
到节点 2
(自己) 的一条边,从而造成自环。咱们将图形可视化以下:
1 / \ / \ 0 --- 2 / \ \_/
2ms
1 /** 2 * Definition for undirected graph. 3 * class UndirectedGraphNode { 4 * int label; 5 * List<UndirectedGraphNode> neighbors; 6 * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 11 if (node == null) return null; 12 Map<Integer, UndirectedGraphNode> map = new HashMap<>(); // 存放节点 13 return dfs(map, node); 14 } 15 16 private UndirectedGraphNode dfs(Map<Integer, UndirectedGraphNode> map, UndirectedGraphNode node) { 17 // 是否存在 存在返回 18 UndirectedGraphNode cloned = map.get(node.label); 19 if (cloned != null) return cloned; 20 // clone一个 21 cloned = new UndirectedGraphNode(node.label); 22 map.put(cloned.label, cloned); 23 // 加入子节点 24 for(UndirectedGraphNode neighbor: node.neighbors) { 25 cloned.neighbors.add(dfs(map,neighbor)); 26 } 27 return cloned; 28 } 29 }
2ms
1 /** 2 * Definition for undirected graph. 3 * class UndirectedGraphNode { 4 * int label; 5 * List<UndirectedGraphNode> neighbors; 6 * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 HashMap<Integer, UndirectedGraphNode> map = new HashMap<>(); 11 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 12 if(node==null) 13 return null; 14 15 UndirectedGraphNode head = new UndirectedGraphNode(node.label); 16 map.put(head.label, head); 17 18 for(UndirectedGraphNode nei: node.neighbors) { 19 // UndirectedGraphNode nei1 = null; 20 UndirectedGraphNode nei1 = map.get(nei.label); 21 if(nei1==null) { 22 head.neighbors.add(cloneGraph(nei)); 23 } 24 else 25 head.neighbors.add(nei1); 26 } 27 return head; 28 } 29 }
4ms
1 /** 2 * Definition for undirected graph. 3 * class UndirectedGraphNode { 4 * int label; 5 * List<UndirectedGraphNode> neighbors; 6 * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 11 if (node == null) { 12 return null; 13 } 14 Map<UndirectedGraphNode, UndirectedGraphNode> visited = new HashMap<>(); 15 visited.put(node, new UndirectedGraphNode(node.label)); 16 dfs(node, visited); 17 return visited.get(node); 18 } 19 20 private void dfs(UndirectedGraphNode node, Map<UndirectedGraphNode, UndirectedGraphNode> visited) { 21 for (UndirectedGraphNode nei : node.neighbors) { 22 if (!visited.containsKey(nei)) { 23 visited.put(nei, new UndirectedGraphNode(nei.label)); 24 dfs(nei, visited); 25 } 26 visited.get(node).neighbors.add(visited.get(nei)); 27 } 28 29 } 30 }
5ms
1 /** 2 * Definition for undirected graph. 3 * class UndirectedGraphNode { 4 * int label; 5 * List<UndirectedGraphNode> neighbors; 6 * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 11 if(node == null) return null; 12 13 Map<Integer, UndirectedGraphNode> valToNode = new HashMap<>(); 14 Queue<UndirectedGraphNode> queue = new LinkedList<>(); 15 queue.add(node); 16 17 while(!queue.isEmpty()){ 18 UndirectedGraphNode current = queue.poll(); 19 UndirectedGraphNode copiedNode = valToNode.getOrDefault(current.label, new UndirectedGraphNode(current.label)); 20 valToNode.put(current.label, copiedNode); 21 22 for(UndirectedGraphNode neighbor : current.neighbors){ 23 UndirectedGraphNode copiedNeighbor = valToNode.getOrDefault(neighbor.label, new UndirectedGraphNode(neighbor.label)); 24 copiedNode.neighbors.add(copiedNeighbor); 25 26 if(!valToNode.containsKey(neighbor.label)){ 27 queue.add(neighbor); 28 valToNode.put(neighbor.label, copiedNeighbor); 29 } 30 } 31 } 32 33 return valToNode.get(node.label); 34 } 35 }