【Lintcode】137.Clone Graph
题目:
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 #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(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的更多相关文章
- 【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 ...
- 【leetcode】133. Clone Graph
题目如下: Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph con ...
- 【LeetCode】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【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 ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 论文阅读笔记(十八)【ITIP2019】:Dynamic Graph Co-Matching for Unsupervised Video-Based Person Re-Identification
论文阅读笔记(十七)ICCV2017的扩刊(会议论文[传送门]) 改进部分: (1)惩罚函数:原本由两部分组成的惩罚函数,改为只包含 Sequence Cost 函数: (2)对重新权重改进: ① P ...
- 【POJ】【2125】Destroying the Graph
网络流/二分图最小点权覆盖 果然还是应该先看下胡伯涛的论文…… orz proverbs 题意: N个点M条边的有向图,给出如下两种操作.删除点i的所有出边,代价是Ai.删除点j的所有入边,代价是Bj ...
随机推荐
- Android 开源项目精选
0x00 leakcanary [内存泄漏检测] Leakcanary : A memory leak detection library for Android and Java. 良心企业Squ ...
- 【C语言天天练(二)】statickeyword
引言: statickeyword不仅能够修饰变量.并且能够修饰函数.了解它的使用方法,不仅对阅读别人的代码有帮助,也有助于自己写出更加健壮的程序. 使用方法: ...
- Spring Data JPA 事务锁
1.概述 在本快速教程中,我们将讨论在Spring Data JPA中为自定义查询方法和预定义存储库的CRUD方法启用事务锁, 我们还将查看不同的锁类型并设置事务锁超时. 2.锁类型 JPA定义了两种 ...
- DWR3.0(Direct Web Remoting)实践
“DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and c ...
- java中BigDecimal的学习
干着java的活,但是看的都是一些偏底层的东西(或者我根本就没有看),有点荒废了java的学习. 最近一直在用到一个类是BigDecimal,但都是模棱两可地在那儿用,并没有深入研究这个类的细节,感觉 ...
- JVM完全指南
JVM完全指南 一:虚拟机内存图解 JAVA程序运行与虚拟机之上,运行时需要内存空间.虚拟机执行JAVA程序的过程中会把它管理的内存划分为不同的数据区域方便管理. 虚拟机管理内存数 ...
- nginx配置1:借助Nginx搭建反向代理服务器与缓存静态文件
修改配置文件nginx.conf (1)进程数与每个进程的最大连接数: •nginx进程数,建议设置为等于CPU总核心数 •单个进程最大连接数,那么该服务器的最大连接数=连接数*进程数 (2)Ngin ...
- 计算机器内存数量+引入和显示ARDS成员
[1]README 1.1) 本代码在于读取内存中多个 内存段的地址范围描述符结构体(ARDS),有多少个内存段可以用: 1.2) source code and images in the blog ...
- python网络爬虫之初识网络爬虫
第一次接触到python是一个很偶然的因素,由于经常在网上看连载小说,很多小说都是上几百的连载.因此想到能不能自己做一个工具自动下载这些小说,然后copy到电脑或者手机上,这样在没有网络或者网络信号不 ...
- Android学习之——优化篇(2)
一.高级优化 上篇主要从0基础优化的方式,本篇主要将从程序执行性能的角度出发,分析各种经常使用方案的不足.并给出对象池技术.基础数据类型替换法.屏蔽函数计算三种能够节省资源开销和处理器时间的优 ...