【转载请注明】: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. jQuery通过event获取点击事件的事件对象

    要想搞明白js的事件机制,必须搞清楚几个概念:事件对象,事件源,还有事件流 事件对象: 当事件发生时会产生事件对象,事件对象的作用是用来记录“事件发生是一些相关的信息.注意事件对象只有在事件发生时才会 ...

  2. rpm 命令的使用

    rpm -ivh    xv-3.10a-13.i386.rpm 在Terminal中,基本的安装指令如下: rpm -i    xv-3.10a-13.i386.rpm 如果你的连网速度足够快,也可 ...

  3. 为什么说Ubuntu的运行级别为2

    继上一篇文章http://www.cnblogs.com/EasonJim/p/7163069.html深入研究了Linux的运行级别之后,发现网上大部分都说Ubuntu的运行级别默认为2,那么下面就 ...

  4. pg_dump: [archiver (db)] connection to database “dbase” failed: FATAL: Peer authentication failed for user “postgres”

    "Peer authentication" means that it's comparing your database username against your Linux ...

  5. ROBODK仿真如何设置运动速度

    设置工具-选项-运动,把仿真时间设置成跟正常一样   然后双击机器人,设置参数(可以设置movej和movel的速度,加速度)  

  6. VS2012调试执行,网页打不开

    360修复漏洞篇 TODO 修复了漏洞.vs2012在firefox和ie中都打不开 解决思路:360漏洞修复→已安装漏洞→卸载刚刚安装的漏洞 就可以解决 忽略漏洞 正常打开.

  7. ACdream 1125(ACfun-字典序)

    A - ACfun Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...

  8. Codeforces #2B The least round way(DP)

    Description 有一个n*n的正整数矩阵,要你求一条从第一行第一列的格子到第n行第n列的路,使得你走过的格子里面的数乘起来的值末尾的零的个数最小.输出最小个数. Input 第一行包括1个数n ...

  9. Apache Kafka-0.8.1.1源代码编译

    作者:过往记忆 | 新浪微博:左手牵右手TEL | 能够转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明博客地址:http://www.iteblog.com/文章标题:<Apac ...

  10. luogu1040 加分二叉树

    题目大意 设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第j个节点的分数为di,tree及它的每个子树都 ...