【转载请注明】: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. Sigmoid Function

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51734189 Sigmodi 函数是一 ...

  2. 有一张表里面有上百万的数据,在做查询的时候,如何优化?从数据库端,java端和查询语句上回答

    原文:https://www.2cto.com/database/201612/580140.html 1)数据库设计方面: a. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 o ...

  3. Uva12657 Boxes in a Line

    题目链接:传送门 分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了 ...

  4. [USACO1.2]挤牛奶Milking Cows

    题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开 ...

  5. hdu3303

    分析:一个最暴力的想法是把加入到集合S的数据一个个按顺序保存起来,然后每次查询的时候由后向前计算余数,如果遇到余数为0的,就直接把时间输出,否则就一直比较到最后找余数最小时间最晚的,这样查询的时间复杂 ...

  6. Centos7 上安装mysql遇上的问题:mysql无法正常启动

    第一次在Centos上安装mysql遇到的一些问题. 第一步就遇到问题,安装mysql-server报错没有可用包.  [解决方法] 先要安装mysql # wget http://repo.mysq ...

  7. iOS: 学习笔记, Swift操作符定义

    Swift操作符能够自行定义, 仅仅须要加上简单的标志符就可以. @infix 中置运算. 如+,-,*,/运算 @prefix 前置运算. 如- @postfix 后置运算. a++, a-- @a ...

  8. Codeforces 196 D. The Next Good String

    D. The Next Good String time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. 转 java面试题

    ● 简述synchronized?Object:Monitor机制: ● 简述happen-before规则 : ● JUC和Object : Monitor机制区别是什么 : 简述AQS原理 : ● ...

  10. Selenium中配置链接使用FTP服务

    Enable the default report solution Step1: Create a suite listener and add codes into it, please watc ...