LeetCode: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
- / \
- \_/
分析:BFS或者DFS遍历图,遍历的过程中复制节点,用哈希表保存新建立的节点的地址(同时这个哈希表可以用做节点访问标志)代码如下: 本文地址
- /**
- * Definition for undirected graph.
- * struct UndirectedGraphNode {
- * int label;
- * vector<UndirectedGraphNode *> neighbors;
- * UndirectedGraphNode(int x) : label(x) {};
- * };
- */
- class Solution {
- public:
- UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
- // IMPORTANT: Please reset any member data you declared, as
- // the same Solution instance will be reused for each test case.
- typedef unordered_map<int, UndirectedGraphNode *> Map;
- if(node == NULL)return NULL;
- Map gmap;//保存克隆图的节点的地址,顺便作为节点是否访问的标记
- stack<UndirectedGraphNode *>gstack;
- UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
- gmap.insert(Map::value_type(res->label, res));
- gstack.push(node);
- while(gstack.empty() == false)
- {
- UndirectedGraphNode *p = gstack.top(), *newp;
- gstack.pop();
- if(gmap.find(p->label) != gmap.end())//查找克隆图节点是否已经构造
- newp = gmap[p->label];
- else
- {
- newp = new UndirectedGraphNode(p->label);
- gmap.insert(Map::value_type(p->label, newp));
- }
- for(int i = ; i < p->neighbors.size(); i++)
- {
- UndirectedGraphNode *tmp = p->neighbors[i];
- if(gmap.find(tmp->label) == gmap.end())
- {
- gmap.insert(Map::value_type(tmp->label,
- new UndirectedGraphNode(tmp->label)));
- gstack.push(tmp);
- }
- //设置克隆图节点的邻接点
- newp->neighbors.push_back(gmap[tmp->label]);
- }
- }
- return res;
- }
- };
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3418412.html
LeetCode:Clone Graph的更多相关文章
- LeetCode: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- [leetcode]Clone Graph @ Python
原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [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 ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
随机推荐
- Jmeter调试工具---Debug Sampler
一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...
- 为Asp.net MVC中的RenderSection设置默认内容
1. RenderSection的简单介绍 Asp.net MVC中提供了RenderSection方法,这样就能够在Layout中定义一些区块,这些区块留给使用Layout的view来实现比如我们定 ...
- SpringMVC 返回 html 视图页面,SpringMVC与Servlet,Servlet重定向与转发
1. SpringMVC与Servlet的关系 SpringMVC框架是建立在Servlet之上的,提供各种功能,各种封装,各种方便的同时,它一点儿也没有限制Servlet,我们完全可以在Spring ...
- VISUAL STUDIO 调试
调试术语 Visual Studio调试之断点基础篇 Visual Studio调试之断点进阶篇 不能设置断点的检查步骤 Visual Studio调试之断点技巧篇 Visual Studio调试之断 ...
- oracle树操作(select start with connect by prior)
oracle中的递归查询可以使用:select .. start with .. connect by .. prior 下面将会讲述oracle中树形查询的常用方式,只涉及到一张表. 一. 建表语句 ...
- avahi-daemon启动失败-解决方法-linux
avahi-daemon是一种Linux操作系统上运行在客户机上实施查找基于网络的Zeroconf service的服务守护进程. 该服务可以为Zeroconf网络实现DNS服务发现及DNS ...
- DataGridView合并单元格
昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,然后直接使用e.Graphics.FillRectangle(backColorBru ...
- [转帖]迅为4412开发板最小linux系统的存储空间修改
本文转自迅为论坛:http://www.topeetboard.com 最小linux系统的存储空间修改以修改成 1G 存储空间为例来修改,如果需要改成其他大小的存储空间,参照此方法修改即可. 首先连 ...
- nopcommerce3.3简洁版
从nopcommerce里面分离出了基础框架,包括了用户.新闻.单页面.投票等模块,可以作为快速开发asp.net mvc项目的方案,有兴趣的朋友可以下载看看,由于时间仓促可能会有一些多余的文件没有清 ...
- java 字节流和字符流的区别 转载
转载自:http://blog.csdn.net/cynhafa/article/details/6882061 java 字节流和字符流的区别 字节流与和字符流的使用非常相似,两者除了操作代码上的不 ...