题目:

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. caffe--anaconda2--makefile.config--compile --ubuntu16.04

    sea@sea-X550JK:/media/sea/wsWin10/wsUbuntu16.04/DlFrames/caffe$ cat Makefile.config: ## Refer to htt ...

  2. EasyUI触发方法、触发事件、创建对象的格式??

    创建对象 $("选择器").组件名({ 属性名 : 值, 属性名 : 值 }); 触发方法 $("选择器").组件名("方法名",参数); ...

  3. 在hibernate3中如何利用HQL语句查询出对象中的部分数据并且返回该对象?

    例如现在有一个Customer对象 public class Customer{ private Integer cid; private String cname; private Integer ...

  4. 五分钟了解 Service Mesh

      1 背景   1.1 多语言   微服务理念是提倡不同业务使用最适合它的语言开发,现实情况也确实如此,尤其是AI的兴起,一般大型互联网公司存在 C/C++.Java.Golang.PHP.Pyth ...

  5. Linux下服务端口被占用

    有一次,在启动ejabberd的时候,报错如下: 10:30:15 =CRASH REPORT==== crasher: initial call: supervisor:ejabberd_liste ...

  6. Drupal 主题的表现形式

    1.template.php /**  * Implements hook_theme().  */ function yourtheme_theme($existing, $type, $theme ...

  7. Codeforces 390E Inna and Large Sweet Matrix 树状数组改段求段

    题目链接:点击打开链接 题意:给定n*m的二维平面 w个操作 int mp[n][m] = { 0 }; 1.0 (x1,y1) (x2,y2) value for i : x1 to x2 for ...

  8. EasyRTMP+EasyDSS实现一套完整的紧急视频回传直播与存储回放方案

    需求来源 紧急视频回传云端:即拍即传.云端存储.紧急录像.云拍云录!这些需求现在可能对于我们来说比较远,大部分也是在行业中才会用到,但相信在不就的将来肯定会落地到每个人的手中,因为这是一个自我保护.自 ...

  9. 【题解】CF1103D Professional layer

    [题解]CF1103DProfessional layer 神题做前先\(orzyyb\) 一个很好的性质(之前也见过但是没有想到的) zhengchu \(gcd\le 10^{12}\) 所以不同 ...

  10. Why Use C++/CLI?

    来源:http://www.asawicki.info/Download/Productions/Publications/CPP_CLI_tutorial.pdf Why Use C++/CLI? ...