[LeetCode] Clone Graph 无向图的复制
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a val (int
) and a list (List[Node]
) of its neighbors.
Example:
Input:
{"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1} Explanation:
Node 1's value is 1, and it has two neighbors: Node 2 and 4.
Node 2's value is 2, and it has two neighbors: Node 1 and 3.
Node 3's value is 3, and it has two neighbors: Node 2 and 4.
Node 4's value is 4, and it has two neighbors: Node 1 and 3.
Note:
- The number of nodes will be between 1 and 100.
- The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.
- Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.
- You must return the copy of the given node as a reference to the cloned graph.
这道无向图的复制问题和之前的 Copy List with Random Pointer 有些类似,那道题的难点是如何处理每个结点的随机指针,这道题目的难点在于如何处理每个结点的 neighbors,由于在深度拷贝每一个结点后,还要将其所有 neighbors 放到一个 vector 中,而如何避免重复拷贝呢?这道题好就好在所有结点值不同,所以我们可以使用 HashMap 来对应原图中的结点和新生成的克隆图中的结点。对于图的遍历的两大基本方法是深度优先搜索 DFS 和广度优先搜索 BFS,这里我们先使用深度优先搜索DFS来解答此题,在递归函数中,首先判空,然后再看当前的结点是否已经被克隆过了,若在 HashMap 中存在,则直接返回其映射结点。否则就克隆当前结点,并在 HashMap 中建立映射,然后遍历当前结点的所有 neihbor 结点,调用递归函数并且加到克隆结点的 neighbors 数组中即可,代码如下:
解法一:
class Solution {
public:
Node* cloneGraph(Node* node) {
unordered_map<Node*, Node*> m;
return helper(node, m);
}
Node* helper(Node* node, unordered_map<Node*, Node*>& m) {
if (!node) return NULL;
if (m.count(node)) return m[node];
Node *clone = new Node(node->val);
m[node] = clone;
for (Node *neighbor : node->neighbors) {
clone->neighbors.push_back(helper(neighbor, m));
}
return clone;
}
};
我们也可以使用 BFS 来遍历图,使用队列 queue 进行辅助,还是需要一个 HashMap 来建立原图结点和克隆结点之间的映射。先克隆当前结点,然后建立映射,并加入 queue 中,进行 while 循环。在循环中,取出队首结点,遍历其所有 neighbor 结点,若不在 HashMap 中,我们根据 neigbor 结点值克隆一个新 neighbor 结点,建立映射,并且排入 queue 中。然后将 neighbor 结点在 HashMap 中的映射结点加入到克隆结点的 neighbors 数组中即可,参见代码如下:
解法二:
class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node) return NULL;
unordered_map<Node*, Node*> m;
queue<Node*> q{{node}};
Node *clone = new Node(node->val);
m[node] = clone;
while (!q.empty()) {
Node *t = q.front(); q.pop();
for (Node *neighbor : t->neighbors) {
if (!m.count(neighbor)) {
m[neighbor] = new Node(neighbor->val);
q.push(neighbor);
}
m[t]->neighbors.push_back(m[neighbor]);
}
}
return clone;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/clone-graph/
https://leetcode.com/problems/clone-graph/discuss/42313/C%2B%2B-BFSDFS
https://leetcode.com/problems/clone-graph/discuss/42309/Depth-First-Simple-Java-Solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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: 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 ...
- [leetcode]Clone Graph @ Python
原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...
- 133. Clone Graph(图的复制)
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 【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 ...
随机推荐
- SpringMVC一路总结(三)
在博文<SpringMVC一路总结(一)>和<SpringMVC一路总结(二)>中,该框架的应用案例都是是基于xml的形式实现的.然而,对于大型项目而言,这种xml的配置会增加 ...
- heart
好久没写博客了,不想废话,直接欣赏效果! 点击这里,查看完美效果! 附完整代码: <!doctype html> <html> <head> <meta ch ...
- 深度解析C语言int与unsigned int
就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...
- 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(三)
(三) 搭建Windows Azure Pack环境 1安装SQL SERVER 2012 服务器 为简单起见,本例直接使用了Azure提供的具有SQLServer的Win2012 Server镜像来 ...
- Xcode7.1环境下上架iOS App到AppStore 流程③(Part 三)
前言部分 part三 部分主要讲解 Xcode关联绑定发布证书的配置.创建App信息.使用Application Loader上传.ipa文件到AppStore 一.Xcode配置发布证书信息 1)给 ...
- C语言辗转相除法求2个数的最小公约数
辗转相除法最大的用途就是用来求两个数的最大公约数. 用(a,b)来表示a和b的最大公约数. 有定理: 已知a,b,c为正整数,若a除以b余c,则(a,b)=(b,c). (证明过程请参考其它资料) 例 ...
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- 课堂Java小程序(加减乘除与验证码)
一.编写一个程序,用户输入两个数,求出其加减乘除,并用消息框 显示计算结果. 1.设计思想:从键盘输入两个数字和运算符,然后计算.将输入的数字及运算符由字符型转换为整型,再用if判断输入的运算符,根据 ...
- JDBC介绍
1.DriverManager用来建立和数据库的链接以及管理JDBC驱动程序 driverManager的常用方法 方法 描述 registerDriver(Driver driver) 在Derve ...
- windows go安装
1.安装git 因为golang是通过git来管理远程包的,所以我们首先要安装git,下载地址:http://www.git-scm.com/download/. git安装比较简单,直接下一步即可( ...