★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9963297.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
Nodes are labeled uniquely.
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}
.
The graph has a total of three nodes, and therefore contains three parts as separated by #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/
/
0 --- 2
/
\_/
克隆一张无向图,图中的每个节点包含一个 label
(标签)和一个 neighbors
(邻接点)列表 。
OJ的无向图序列化:
节点被唯一标记。
我们用 #
作为每个节点的分隔符,用 ,
作为节点标签和邻接点的分隔符。
例如,序列化无向图 {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 }