原题链接在这里:https://leetcode.com/problems/clone-graph/description/
题目:
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 / \ \_/
题解:
复制原来的graph, 输入是一个graph node, 返回也是一个 graph node.
可以利用BFS, 同时维护HashMap hm 来记录visited 过的点. 以及 original node 和 copy node 的关系。
key存储原graph的node, value存用原node.label复制出来的node. 检查这个neighbor是不是已经在hm的key中了,如果已经在了就跳过,如果没在就加到queue中,并建立一个自身的copy组成键值对放入hm中。
同时更新当前节点currentNode 复制点 的neighbors list. 此时无论它的neighbor是否在hm中都应该把hm中对应这个heighbor key 的 value 放到 list中。
最后返回 以输入点的label复制的点。
Time Complexity: O(N+E). Space: O(N).
AC Java:
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 node; 13 } 14 15 UndirectedGraphNode cloneNode = new UndirectedGraphNode(node.label); 16 HashMap<UndirectedGraphNode, UndirectedGraphNode> visited = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); 17 visited.put(node, cloneNode); 18 19 LinkedList<UndirectedGraphNode> que = new LinkedList<UndirectedGraphNode>(); 20 que.add(node); 21 while(!que.isEmpty()){ 22 UndirectedGraphNode cur = que.poll(); 23 for(UndirectedGraphNode neighbor : cur.neighbors){ 24 if(!visited.containsKey(neighbor)){ 25 UndirectedGraphNode cloneNeighbor = new UndirectedGraphNode(neighbor.label); 26 visited.put(neighbor, cloneNeighbor); 27 que.add(neighbor); 28 } 29 visited.get(cur).neighbors.add(visited.get(neighbor)); 30 } 31 } 32 33 return cloneNode; 34 } 35 }
也可以DFS.
Time Complexity: O(N+E). Space: O(N).
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 private HashMap<UndirectedGraphNode, UndirectedGraphNode> visited = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); 11 12 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 13 if(node == null){ 14 return node; 15 } 16 17 if(visited.containsKey(node)){ 18 return visited.get(node); 19 } 20 21 UndirectedGraphNode cloneNode = new UndirectedGraphNode(node.label); 22 visited.put(node, cloneNode); 23 24 for(UndirectedGraphNode neighbor : node.neighbors){ 25 cloneNode.neighbors.add(cloneGraph(neighbor)); 26 } 27 28 return cloneNode; 29 } 30 }