21. Clone Graph
Clone Graph
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
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
/ \
\_/ 思想: 简单纯粹的深度优先搜索。
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
typedef UndirectedGraphNode NODE;
typedef pair<NODE*, NODE*> PAIR;
NODE* dfs(NODE *node, unordered_map<NODE*, NODE*> &_map) {
if(_map.count(node)) return _map[node];
NODE *p = new NODE(node->label);
_map.insert(PAIR(node, p));
for(size_t i = 0; i < node->neighbors.size(); ++i) {
p->neighbors.push_back(dfs(node->neighbors[i], _map));
}
return p;
} class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
unordered_map<NODE*, NODE*> _map;
_map[NULL] = NULL;
return dfs(node, _map);
}
};
21. Clone Graph的更多相关文章
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- 【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: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- 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之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
随机推荐
- plsql developer 导出导入存储过程和函数
说明:需要把建表脚本及表数据分开导出,操作很简单.一.导出表及存储过程等对象:1. 登录PL-SQL Developer2. 选择只显示本用户的对象,如下图:3. 选择菜单“Tools——〉Expor ...
- 搭建Tomcat6源代码阅读环境
目标:使用MyEclipse8.5阅读Tomcat6源码. 第一步:在MyEclipse8.5中集成SVN插件. 第二步:从地址http://svn.apache.org/repos/asf/tomc ...
- ios之AFN上传下载详细步骤(2)
五.AFN .GET\POST > GET请求 // 1.获得请求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperation ...
- html5有哪些新特性、移除了那些元素?如何处理HTML5新标签的浏览器兼容问题?如何区分 HTML 和 HTML5?
* HTML5 现在已经不是 SGML(标准通用标记语言,是一种定义电子文档结构和描述其内容的国际标准语言) 的子集,主要是关于图像,位置,存储,多任务等功能的增加. * 拖拽释放(Drag an ...
- 传智博客.NET培训第13季 Ajax教程(共十三季) 学习资源
http://wangpengnimei.ctfile.com/u/1235801/47146 传智博客.NET培训第13季 Ajax教程(十三季).rar 19
- C#异步批量下载文件
C#异步批量下载文件 实现原理:采用WebClient进行批量下载任务,简单的模拟迅雷下载效果! 废话不多说,先看掩饰效果: 具体实现步骤如下: 1.新建项目:WinBatchDownload 2.先 ...
- [转]lua面向对象编程之点号与冒号的差异详细比较
首先,先来一段在lua创建一个类与对象的代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Class = {} Class.__index = Cl ...
- 看啦这么就别人的博客 我也来写一篇! Object转换其他类型
package com.sinitek.framework.util; import java.math.BigDecimal;import java.sql.Timestamp;import jav ...
- 用javaScript实现 登陆记住密码功能。
一.先写一个存取 cookie的方法. function getCookie(cookiename) { var result; var mycookie = document.cookie; var ...
- Linux字符界面的优势
优势一:字符界面占用的系统资源更少,更加适合服务器 优势二:字符界面减少了出错.被攻击的可能性(一.二决定了服务器一般不装图形界面,安全稳定优先)