Clone Graph(LeetCode)

题目:

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
         / \
         \_/

题目分析:题目要求深度遍历一个无向图。图中每一个节点都有一个唯一的label,然后可以有很多的neighbors,其中neighbor也可以是自己。



思路:

采用深度遍历图的方式,一个个节点的复制。为了避免重复复制节点,创建一个HashMap,记录已经创建过的节点。(刚开始使用了原节点node的label做key,所复制节点copy做value,即<node.label,copy>。在LeetCode上总是提示WrongAnswer。但在eclipse上运行同样的输入,能够得到正确的答案。难道是因为版本不同?【LeetCode上是1.7,PC上是1.6】。后来改用<node,copy>作map,就能通过了)

具体算法如下:

  1. 创建一个HashMap记录已经复制过的节点
  2. 从节点node开始,深度遍历图。创建一个copy节点,复制node的label。将<node,copy>放入map中。
  3. 遍历node的neighbors。
    • 如果一个neighbor已在map中,说明其在之前已经被复制。直接将map中对应的节点放入copy的neighbors列表中。
    • 如果一个neighbor不再map中,递归调用cloneGraph来创建节点,并且加入到copy的neighbors列表中
  4. 返回copy




注意点:

  1. 刚开始,创建map使用了<node.label,copy>对。因为对于node,其label也是唯一的。应该没有问题,在eclipse上运行也能给出正确答案。但在LeetCode上,相同输入,却得到一个错误答案。可能是版本不同引起的。也可能是LeetCode本身的问题。后来,其他代码未变,map改用<node,copy>对,LeetCode上也能成功通过。
  2. 例如,输入{0,1#1,2#2,2} , LeetCode上输出是{0,1#1} ,而eclipse上能够得到{0,1#1,2#2,2}这种结果。



代码:

/**
 * Definition for undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */
import java.util.HashMap;
public class Solution {
    static HashMap<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node == null){
            return null;
        }
        
        UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
        map.put(node,copy);
        for (int i = 0; i < node.neighbors.size(); i++){
            UndirectedGraphNode thisNeighbor = node.neighbors.get(i);
            if(map.containsKey(thisNeighbor)){
                copy.neighbors.add(map.get(thisNeighbor));
            }else{
                copy.neighbors.add(cloneGraph(thisNeighbor));
            }
        }
        return copy;
    }
}


未通过的代码:

/**
 * Definition for undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */
import java.util.HashMap;
public class Solution {
    static HashMap<Integer,UndirectedGraphNode> map = new HashMap<Integer,UndirectedGraphNode>();
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node == null){
            return null;
        }
        
        UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
        map.put(copy.label,copy);
        for (int i = 0; i < node.neighbors.size(); i++){
            UndirectedGraphNode thisNeighbor = node.neighbors.get(i);
            if(map.containsKey(thisNeighbor.label)){
                copy.neighbors.add(map.get(thisNeighbor.label));
            }else{
                copy.neighbors.add(cloneGraph(thisNeighbor));
            }
        }
        return copy;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值