题目:

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

How we serialize an undirected graph:

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

Example

return a deep copied graph.

题解:

  DFS(recursion)

Solution 1 ()

class Solution {
public:
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> hash;
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node) return node;
if(hash.find(node) == hash.end()) {
hash[node] = new UndirectedGraphNode(node -> label);
for (auto neighbor : node -> neighbors) {
(hash[node] -> neighbors).push_back( cloneGraph(neighbor) );
}
}
return hash[node];
}
};

  DFS(stack)

Solution 2 ()

class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == nullptr)
return nullptr;
stack<UndirectedGraphNode* > stack; map<int, UndirectedGraphNode* > visitTable;
UndirectedGraphNode* newnode = new UndirectedGraphNode(node->label);
visitTable[node->label] = newnode;
stack.push(node); while (!stack.empty()) {
UndirectedGraphNode* cur = stack.top();
stack.pop();
for (auto neighbor : cur->neighbors) {
if (visitTable.find(neighbor->label) == visitTable.end()) {
stack.push(neighbor);
UndirectedGraphNode* newneighbor = new UndirectedGraphNode(neighbor->label);
visitTable[neighbor->label] = newneighbor;
}
visitTable[cur->label]->neighbors.push_back(visitTable[neighbor->label]);
}
} return newnode;
}
};

  BFS

Solution 3 ()

class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == nullptr)
return nullptr;
queue<UndirectedGraphNode* > queue; map<int, UndirectedGraphNode* > visitTable;
UndirectedGraphNode* newnode = new UndirectedGraphNode(node->label);
visitTable[node->label] = newnode;
queue.push(node); while (!queue.empty()) {
UndirectedGraphNode* cur = queue.front();
queue.pop();
for (auto neighbor : cur->neighbors) {
if (visitTable.find(neighbor->label) == visitTable.end()) {
UndirectedGraphNode* newneighbor = new UndirectedGraphNode(neighbor->label);
visitTable[neighbor->label] = newneighbor;
queue.push(neighbor);
}
visitTable[cur->label]->neighbors.push_back(visitTable[neighbor->label]);
}
} return newnode;
}
};

【Lintcode】137.Clone Graph的更多相关文章

  1. 【LeetCode】133. Clone Graph (3 solutions)

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  2. 【leetcode】133. Clone Graph

    题目如下: Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph con ...

  3. 【LeetCode】133. Clone Graph 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  5. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  6. 【Git】git clone报错 git fatal: Unable to find remote helper for 'https'

    [参考资料] https://stackoverflow.com/questions/8329485/unable-to-find-remote-helper-for-https-during-git ...

  7. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  8. 论文阅读笔记(十八)【ITIP2019】:Dynamic Graph Co-Matching for Unsupervised Video-Based Person Re-Identification

    论文阅读笔记(十七)ICCV2017的扩刊(会议论文[传送门]) 改进部分: (1)惩罚函数:原本由两部分组成的惩罚函数,改为只包含 Sequence Cost 函数: (2)对重新权重改进: ① P ...

  9. 【POJ】【2125】Destroying the Graph

    网络流/二分图最小点权覆盖 果然还是应该先看下胡伯涛的论文…… orz proverbs 题意: N个点M条边的有向图,给出如下两种操作.删除点i的所有出边,代价是Ai.删除点j的所有入边,代价是Bj ...

随机推荐

  1. Xenomai 3 POSIX

    Xenomai 3在架构设计上确实优先Xenomai 2,至少对开发者来说,少维护了不少东西,看下面两张图就知道了 第一张图是Xenomai2的,第二张图是Xenomai3的,Xenomai3在内核中 ...

  2. 深入浅出Attribute(三)

    约定: 1.”attribute”和”attributes”均不翻译 2.”property”译为“属性” 3.msdn中的原句不翻译 4.”program entity”译为”语言元素” Attri ...

  3. java 泛型小小的测试题

    判断以下哪种书写时正确的? 1.ArrayList<String> lists = new ArrayList<String>();2.ArrayList<Object& ...

  4. 在VMware下安装CentOS系列1:配置VMware

    安装环境 VMware Workstation v9.0.0 build-812388 CentOS-6.3-x86_64-minimal.iso minimal,bin-DVD,netinstall ...

  5. 【转】基于eclipse进行ndk开发的环境配置

    前述虽然我们在其他的博文中(如https://blog.csdn.net/ericbar/article/details/76602720),早就用到了ndk,但如果想在Android设备运行包含这些 ...

  6. 不错的iOS相关的主页或站点 (更新于14-06-22)

    近期一直没事在翻一些站点看看资料学习下. 推荐几个不错的站点: http://www.raywenderlich.com/   这个站点有各种各样的教程,可惜是大部分都是英文教程,只是阅读起来还好.每 ...

  7. WCF: 以Json格式返回对象

    1.先建一个WCF Service 建一个ServiceContract接口 1 [ServiceContract] public interface IJsonWCFService { /// &l ...

  8. VSCode 配置python

    https://code.visualstudio.com/docs/?dv=win  下载64位的vscode 底下有python插件

  9. MySQL重置root用户密码的方法【亲测可用】

    1. 报错截图 2.当确认已经忘记MySQL密码,则可以通过以下方案重置root用户密码.双击打开C:\Program Files\MySQL\MySQL Server 5.1\my.ini文件,如下 ...

  10. 【BZOJ4173】数学 欧拉函数神题

    [BZOJ4173]数学 Description Input 输入文件的第一行输入两个正整数 . Output 如题 Sample Input 5 6 Sample Output 240 HINT N ...