LeetCode 133. Clone Graph

原题链接在这里: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.

OJ's undirected graph serialization:

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 #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (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 }

类似Copy List with Random PointerEmployee Importance.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4865742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值