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 node0to both nodes1and2. - Second node is labeled as
1. Connect node1to node2. - Third node is labeled as
2. Connect node2to 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 ...
随机推荐
- javascript 的默认对象
一.日期对象 格式 : 日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用) 2.英文-参数格式 ...
- 怎样查看linux版本
玩一台新的linux服务器,首先要做的,就是先看下是什么版本的系统: 命令如下: 登录到linux执行cat /etc/redhat-release ,例如如下: [root@3.5.5Biz-46 ...
- Netlog 的数据库及 LAMP 架构
Database Sharding@Netlog 详细的描述了 Netlog 数据库架构的演变过程,文章浅显易懂,非常值得学习.本文数据.图片均来自:Database Sharding at Netl ...
- 最锋利的Visual Studio Web开发工具扩展:Web Essentials详解(转)
Web Essentials是目前为止见过的最好用的VS扩展工具了,具体功能请待我一一道来. 首先,从Extension Manager里安装:最新版本是19号发布的2.5版 然后重启你的VS开发环境 ...
- C语言杂谈(三)存储类别
本文讨论C语言中的存储类别,包括数据在内存的存储.变量的存储类别.函数的存储类别.生存周期.下图为计算机的存储空间,有寄存器和内存. 一.存储区域 1.寄存器:存放立即参加运算的数据. 2.系统区:存 ...
- Android分步注册,Activity由B返回A修改再前往B,B中已填项不变
某日突然想到标题问题,一般来说返回上一个Activity,当前Activity应该自动销毁.要想保留值,便想到用bundle传递的方式 最后功能是实现了,但感觉方法很笨. 主要代码如下: packag ...
- C中不安全函数
C 中大多数缓冲区溢出问题可以直接追溯到标准 C 库.最有害的罪魁祸首是不进行自变量检查的.有问题的字符串操作(strcpy.strcat.sprintf 和 gets).一般来讲,象“避免使用 st ...
- virtualbox 在window10上的兼容性调整
更新完windows10后,打开当时的virtualbox 4.3.3已经是最新的啦,打开原来安装的几个虚拟机(hadoop),发现均失败. 打开setting一看,网络一栏有问题,桥接模式的虚拟机都 ...
- DimDate populate data
日期维度 任何一个数据仓库都应该有一个日期维度. 因为很少有不需要通过日期维度看数据的情况存在. 日期维度的好处是,你可以通过他连接各个事实表,然后在报表端传送报表参数的时候, 直接自动过滤日期维度的 ...
- 数据库系统概论(第5版) P262 例8.12
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <Windows.h& ...