Leetcode0133--Clone Graph 克隆无向图
【转载请注明】:https://www.cnblogs.com/igoslly/p/9699791.html
一、题目

二、题目分析
给出一个无向图,其中保证每点之间均有连接,给出原图中的一个点 node,进行图的复制
注意
- 每个点会与多个其他点进行连接,关注节点数据结构,其中 label 表数值,vector 表连接的节点集合
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
- 图克隆≠图复制,理解浅拷贝 & 深拷贝
- 浅拷贝 ——只是对指针的拷贝,拷贝后两个指针指向同一个内存空间
- 深拷贝 ——不但对指针进行拷贝,而且对指针指向的内容进行拷贝,经深拷贝后的指针是指向两个不同地址的指针。
- 对于每个节点vector中值,都必须链接到新图的对应节点上
UndirectedGraphNode * newnode = new UndirectedGraphNode (node->label); // 另创结点,深拷贝
unordered_map<UndirectedGraphNode *,UndirectedGraphNode *> hash; // 建立原node → 新node链接
1. 深度优先遍历dfs,采用递归
2. 广度优先遍历bfs
三、代码解析
1、深度优先搜索
- 以 map 记录新旧对应结点
- 若 map 包含结点,直接加入或链接
- 若 map 不包含结点,创建并递归该结点各项内容
class Solution {
public:
// 递归函数
UndirectedGraphNode *clone(UndirectedGraphNode *node,map<UndirectedGraphNode *,UndirectedGraphNode *>& record){
if(!node) return nullptr;
if(record.find(node)!=record.end()){
return record[node];
}else{
UndirectedGraphNode * temp = new UndirectedGraphNode(node->label);
record[node]=temp;
int size = node->neighbors.size();
for(int i=0;i<size;i++){
temp->neighbors.push_back(clone(node->neighbors[i],record));
}
return temp;
}
}
// 主函数
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
map<UndirectedGraphNode *,UndirectedGraphNode *> record;
UndirectedGraphNode * newnode = clone(node,record);
return newnode;
}
};
2、简化后
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 x : node -> neighbors) {
(hash[node] -> neighbors).push_back( cloneGraph(x) );
}
}
return hash[node];
}
};
2、广度优先搜索
- 以 map 记录新旧对应结点
- 若 map 包含结点,直接加入或链接
- 若 map 不包含结点,加入queue 进行后续操作
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node) return nullptr;
map<UndirectedGraphNode *,UndirectedGraphNode *> record;
queue<UndirectedGraphNode *> nodelist;
nodelist.push(node);
// 队列循环
while(!nodelist.empty()){
UndirectedGraphNode * temp = nodelist.front();nodelist.pop();
UndirectedGraphNode * newtemp;
// 是否已经被创建
if(record.find(temp)==record.end()){
newtemp = new UndirectedGraphNode(temp->label);
record[temp]=newtemp;
}else{
newtemp = record[temp];
}
int size = temp->neighbors.size();
for(int i=0;i<size;i++){
UndirectedGraphNode * child = temp->neighbors[i];
if(record.find(child)==record.end()){
// 连接结点
UndirectedGraphNode * newchild = new UndirectedGraphNode(child->label);
record[child]=newchild;
nodelist.push(child);
newtemp->neighbors.push_back(newchild);
}else{
newtemp->neighbors.push_back(record[child]);
}
}
}
return record[node];
}
};
Leetcode0133--Clone Graph 克隆无向图的更多相关文章
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- [LeetCode] 133. Clone Graph 克隆无向图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [leetcode]133. Clone Graph 克隆图
题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node neighbor 1 2, 3 2 1 3 1 ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- 【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 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 ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
随机推荐
- 使用pymongo.find查询很慢的解决方式
1.前言: mongodb部署在阿里云服务器, mongodb中collection存储了百万条记录. 需求:优化查询指定时间段内的所有数据的查询时间,结果有百万级别. 最初:313587条记录耗时: ...
- 20180710使用gh
转自:http://www.ywnds.com/?p=14265 一.背景 GitHub正式宣布以开源的方式发布gh-ost:GitHub的MySQL无触发器在线更改表定义工具!下面是官方给出gh-o ...
- C. Hexadecimal's Numbers
C. Hexadecimal's Numbers time limit per test 1 second memory limit per test 64 megabytes input stand ...
- 1043 方格取数 2000 noip 提高组
1043 方格取数 2000 noip 提高组 题目描述 Description 设有N*N的方格图(N<=10,我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0.如下图所示(见样 ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- error: 'for' loop initial declarations are only allowed in C99 mode
error: 'for' loop initial declarations are only allowed in C99 mode 使用gcc编译代码是报出 error: 'for' loop i ...
- NoSql的易扩展性
NoSql现在很火很时髦,大家言必称NoSql,仿佛关系型数据库已成陈旧落后的代名词. 但依我看,真正理解NoSql的还不多,在实际项目中用过的应该就更少了. 我也还不理解,更没怎么应用过,所以现在要 ...
- HDU 5489 Difference of Clustering 图论
Difference of Clustering Problem Description Given two clustering algorithms, the old and the new, y ...
- 1695 windows 2013
1695 windows 2013 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 话说adam ...
- node npm Bower
Download | Node.js https://nodejs.org/en/download/ wget https://nodejs.org/dist/v8.11.3/node-v8.11.3 ...