美文网首页
Leetcode 133 Clone Graph

Leetcode 133 Clone Graph

作者: __LINE__ | 来源:发表于2018-02-25 15:10 被阅读20次

解题报告

题目的要求是clone一个无向图。
这个无向图的表达方式类似于adjacency list,但是不一样的地方在于,不会重复出现edges。
比如0,1,2
0: 1,2
对于1来说
1:2
它的neighbor list中就没有0。所以每个edge只会出现一次。

要求是clone这个graph。那么问题来了
1 How to clone a node?

  1. clone value of this node
  2. clone node's neighbors list
    1> clone one neighbor
    2> add this cloned one to the neighbors list

2 Where should I store the cloned graph? Do I need a dummy node point to the starting node?
Overthinking.
Just think for a single node, if u clone it, return the cloned one. That's what we need.

3 What is minimum subquestion?
Simply to say, for a single node, just copy this node.
Clone details are listed on the question 1.

Then we start to analyze this problem using the pattern:
Input:
node

during the process analysis, we know we should also have another input:
HashMap<Integer, Node>

Output:
clonedNode

Process:
****************Base case**************
what is the base cases?
node == null
what should return? null

self-cycle situation
How to find self-cycle?
cache some things using HashSet? or HashMap? since the label is unique.
what to return?
return self which is already cloned.
So we should use HashMap<Integer, Node>

****************Process**************
Clone curNode, copy its value to clonedNode
Clone its neighbors (recursion call)
add the cloned neighbors to the cloneNode's neighbors list

return clonedNode

public class Solution {
    private HashMap<Integer, UndirectedGraphNode> map;
    private UndirectedGraphNode dfsClone(UndirectedGraphNode curNode, HashMap<Integer, UndirectedGraphNode> map) {
        // base case
        if (curNode == null) {
            return null;
        }
        
        if (map.containsKey(curNode.label)) {  //  self cycle
            return map.get(curNode.label);
        }
        
        // process
        UndirectedGraphNode cloneNode = new UndirectedGraphNode(curNode.label);
        map.put(cloneNode.label, cloneNode);
        
        for (UndirectedGraphNode node : curNode.neighbors) {
            cloneNode.neighbors.add(dfsClone(node, map));
        }
        
        return cloneNode;
    }
    
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        map = new HashMap<>();
        return dfsClone(node, map);
    }
}

相关文章

  • Leetcode图

    133. 克隆图[https://leetcode-cn.com/problems/clone-graph/] 2...

  • Leetcode 133 Clone Graph

    解题报告 题目的要求是clone一个无向图。这个无向图的表达方式类似于adjacency list,但是不一样的地...

  • LeetCode 133 [Clone Graph]

    原题 克隆一张无向图,图中的每个节点包含一个 label 和一个表 neighbors。你的程序需要返回一个经过深...

  • Clone Graph (Leetcode 133)

    DFS Approach: 注意,对于DFS,对map的赋值要在DFS loop开始以前。这样可以避免由于grap...

  • Leetcode 133. Clone Graph

    题意:克隆一个无向图。 思路:因为是克隆所以需要一个map来存储克隆的映射关系,我们可以从给定的第一个节点向它的n...

  • LeetCode 133. Clone Graph

    Clone an undirected graph. Each node in the graph contain...

  • Leetcode133 Clone Graph

    Clone Graph Thinking 实现图的深拷贝. 就像图的遍历一样,也可以用深度优先搜索和宽度优先搜索进...

  • 【leetcode】No.133:clone-graph

    题目描述 Clone an undirected graph. Each node in the graph co...

  • 133. Clone Graph

    第一步加到map里面value是null只是为了去重,下一次遇到就不会进queue

  • 133. Clone Graph

网友评论

      本文标题:Leetcode 133 Clone Graph

      本文链接:https://www.haomeiwen.com/subject/pusrxftx.html