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

分析:
首先,这道题目不能直接返回node,因为需要克隆,也就意味着要为所有的节点心分配空间。
其次,分析节点的元素,包括label和neighbors两项,这意味着完成一个节点需要克隆一个int和一个vector<UndirectedGraphNode *>。
我们根据参数中的node提取出其label值,然后用构造方法生成一个节点,这样就完成了一个节点的生成。然后,根据参数中的node提取
其neighbors中的元素,我们只需要把这些元素加入刚生成的节点中,便完成了第一个节点的克隆。
再次,neighbor元素怎么生成呢?同样需要克隆。这样我们就可以总结出递归的思路。
最后,由于克隆要求每个节点只能有一个备份(其实也只能做到一个备份),所以,对于已经有开辟空间的节点,我们只需要返回已经
存在的节点即可。那怎么做到不重复呢?HashMap!只要我们为每个节点开辟一个空间的时候把这个空间指针保存在HashMap中,便可以
通过查找HashMap来确定当前要克隆的节点是否已经存在,不存在再新开辟空间。

c++代码:

 /**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/ class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
//labeled uniquely so label can be used as the key of map
map<int, UndirectedGraphNode *> hashTable;
return clone(node, hashTable);
}
UndirectedGraphNode * clone(UndirectedGraphNode *node, map<int, UndirectedGraphNode *> & hashTable){
if(node == NULL){
return NULL;
}
//表明这个节点已经创建了,所以没有必要重复创建,只要把已经创建的节点返回即可
if(hashTable.find(node->label) != hashTable.end()){
return hashTable[node->label];
}
//如果没有创建参数中节点,则创建,并添加到hashtable中
UndirectedGraphNode * result = new UndirectedGraphNode(node->label);
hashTable[node->label] = result;
//clone所有的邻节点,并添加到刚clone好的节点的第二个元素vector中
for(int i=; i<node->neighbors.size();i++){
UndirectedGraphNode * newnode = clone(node->neighbors[i], hashTable);
result->neighbors.push_back(newnode);
}
return result;
}
};

LeetCode Algorithm 133_Clone Graph的更多相关文章

  1. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

  2. LeetCode Algorithm 05_Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  4. Leetcode总结之Graph

    package Graph; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections ...

  5. [LeetCode] 785. Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  6. LeetCode 785. Is Graph Bipartite?

    原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...

  7. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  8. leetcode 133. Clone Graph ----- java

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  9. 【leetcode】Clone Graph(python)

    类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...

随机推荐

  1. XML解析——DOM解析

    XML:可扩展性标记语言,主要用来传输和存储数据,相对于HTML的各种标签规范,XML的标签可以让用户根据语义自己进行定义,适用于web传输. JSON和XML的区别: (1).XML定义 扩展标记语 ...

  2. EditPlus 使用技巧以及快捷键

    一边阅读,一边动手吧! 为了达到更好的效果,请你先下载我打包的这个 EditPlus压缩包文件(压缩包文件为绿色的EditPlus2.31英文版,含自动完成文件,高亮语法文件和剪切板代码片断文件,这些 ...

  3. Android studio文件名颜色分别表示含义

    这其实是主要和版本控制工具有关,含义如下: 绿色,已经加入控制暂未提交红色,未加入版本控制蓝色,加入,已提交,有改动白色,加入,已提交,无改动 灰色:版本控制已忽略文件

  4. javafx drag

    public class EffectTest extends Application { @Override public void start(Stage stage) { stage.setTi ...

  5. 【canvas】跟随鼠标的星空连线

    2019-01-23 19:57:38 挂一个比较简单的一个canvas应用,利用CPU进行粒子实时计算,直接面向过程写的 帧动画:浏览器在下一个动画帧安排一次网页重绘,  requestAnimat ...

  6. Linux运维命令总结

    .什么是运维?什么是游戏运维? 1)运维是指大型组织已经建立好的网络软硬件的维护,就是要保证业务的上线与运作的正常, 在他运转的过程中,对他进行维护,他集合了网络.系统.数据库.开发.安全.监控于一身 ...

  7. init进程

    2.Linux下的三个特殊进程 Linux下有三个特殊的进程idle进程(PID=0),init进程(PID=1),和kthreadd(PID=2)idle进程由系统自动创建,运行在内核态idle进程 ...

  8. 【Hello 2018 C】Party Lemonade

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出凑够2^j最少需要花费多少钱. 即试着把第i种物品买2^(j-i)个,看看会不会更便宜 记录在huafei[0..31]中 然 ...

  9. linux6.0系统如何安装portmap

    因为在6.0的系统里,portmap已经改名了.在Redhat或CentOS5中可以使用 service portmap start启动服务,然后在启动nfs服务,实现挂载. 6里面可是试试 serv ...

  10. libssh2进行远程运行LINUX命令

    /** * CSSHClient.h * @file 说明信息.. * DATE February 13 2015 * * @author Ming_zhang */ #ifndef _CSSHCLI ...