【转载请注明】: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 克隆无向图的更多相关文章

  1. [LeetCode] Clone Graph 克隆无向图

    Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...

  2. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  3. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  4. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  5. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  6. 【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 ...

  7. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  8. 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 ...

  9. 21. Clone Graph

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

随机推荐

  1. MySQL导入-导出数据库-mac版

    MySQL导入-导出数据库-mac版 导出数据库-表结构,和数据 mysqldump -u 账号 -p 数据库名 表 > 文件名.sql 例如:mysqldump -u root -p test ...

  2. noip模拟赛 蒜头君打地鼠

    分析:直接一个一个地去暴力枚举分数比较少,我们需要一种比较快的统计一定空间内1的数量,标准做法是前缀和,但是二维前缀和维护的是一个矩形内的值,这个是旋转过的该怎么办?可以把图旋转45°,不过这样比较考 ...

  3. 食物链 2001年NOI全国竞赛

    时间限制: 3 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond   题目描述 Description 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B ...

  4. java数组知识总结(一)//按类

    在线api  目录: 零/数组(基本元素) 1.  声明一个数组 2.  创建一个数组 3.  数组名.length 4.  数组的引用 一/java.lang.reflect.Array     / ...

  5. CodeForces 370C

    这道题其实是挺简单的.首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套.反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不 ...

  6. 洛谷——P1720 月落乌啼算钱

    题目背景 (本道题目木有以藏歌曲……不用猜了……) <爱与愁的故事第一弹·heartache>最终章. 吃完pizza,月落乌啼知道超出自己的预算了.为了不在爱与愁大神面前献丑,只好还是硬 ...

  7. Linux查看设备信息命令

    系统 #查看内核/操作系统/CPU信息 uname -a #查看操作系统版本 head -n 1 /etc/issue #查看CPU信息 cat /proc/cpuinfo #查看计算机名 hostn ...

  8. mybatis mapper文件sql语句传入hashmap参数

    1.怎样在mybatis mapper文件sql语句传入hashmap参数? 答:直接这样写map就可以 <select id="selectTeacher" paramet ...

  9. webservice Connection timed out

    webservice Connection timed out,当发生webservice的链接超时错误时.我想原因无非就是webclient到webservice之间的链接通路发生了异常,那么该怎样 ...

  10. UITableView和UITableViewCell的几种样式

    UITableView和UITableViewCell的几种样式 转至  http://blog.csdn.net/crazyzhang1990/article/details/12503163 一. ...