★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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 }