[Leetcode Week3]Clone Graph
Clone Graph题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/clone-graph/description/
Description
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}.
The graph has a total of three nodes, and therefore contains three parts as separated by #.
1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
2. Second node is labeled as 1. Connect node 1 to node 2.
3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
Solution
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == NULL)
return NULL;
map< UndirectedGraphNode*, UndirectedGraphNode* > hashmap;
queue< UndirectedGraphNode* > nodeq;
nodeq.push(node);
hashmap[node] = new UndirectedGraphNode(node -> label);
UndirectedGraphNode *curNodeHash;
while (!nodeq.empty()) {
UndirectedGraphNode* preNode = nodeq.front();
nodeq.pop();
for (auto curNode : preNode -> neighbors) {
if (hashmap.find(curNode) == hashmap.end()) {
curNodeHash = new UndirectedGraphNode(curNode -> label);
hashmap[curNode] = curNodeHash;
nodeq.push(curNode);
}
(hashmap[preNode] -> neighbors).push_back(hashmap[curNode]);
}
}
return hashmap[node];
}
};
解题描述
在这道题上面还花费了很多的时间,一开始想到的算法就是BFS,但是BFS得到的结果总是WA。不断WA的过程中才想到,一个顶点可以有多条指向自己的边。我一开始是没有想到这个问题,然后使用map模拟邻接矩阵来记录边总是WA。然后到后面查阅了相关资料才发现,用哈希的方式,将原图和克隆图的每一个点一一对应起来是最为准确的,且会自动排除BFS中已经访问的点。另外,对于指向自己的边的记录,使用哈希也能准确反映其内涵:顶点包含的顶点指针vector中的顶点就是当前顶点的指针。另外,很多资料中提到,这类题使用DFS递归运算更为精妙简洁。
[Leetcode Week3]Clone 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 ----- java
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 【leetcode】Clone Graph(python)
类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...
- Java for 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#133 Clone Graph
原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- 【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 ...
随机推荐
- ES5新增数组方法(3):some
检查数组元素中是否有元素符合指定. // 数组中的元素部分满足指定条件返回true let arr = [1, 3, 5, 7, 9]; console.log(arr.some((value, in ...
- java.sql.Date java.sql.Time java.sql.Timestamp 之比较
java.sql.Date,java.sql.Time和java.sql.Timestamp 三个都是java.util.Date的子类(包装类). java.sql.Date是java.util.D ...
- win10激活方法-专业版
该教程操作思路,Win10家庭版升为专业版,然后激活版本 首先,进入设置→关于看到如下页面: 接着,进入实操阶段: 第一步 在激活版面输入密匙 ( 把家庭版升级为专业版 ) DR9VN-GF3 ...
- UVa 294 - Divisors 解题报告 c语言实现 素数筛法
1.题目大意: 输入两个整数L.H其中($1≤L≤H≤10^9,H−L≤10000$),统计[L,H]区间上正约数最多的那个数P(如有多个,取最小值)以及P的正约数的个数D. 2.原理: 对于任意的一 ...
- VC++之运算符重载简单小结
封装继承和多态是面向对象三大基本支柱.在面向对象系统中有两种编译方式:静态联编和动态联编静态联编:也叫早期联编:指系统在编译时就决定如何实现某一动作,提供了执行速度快的优点.动态联编:也叫滞后联编:指 ...
- python基础之列表解析
python列表解析:是一个让人欣喜的术语,你可以在一行使用一个for循环将所有的值放在一个列表之中.python列表解析属于python的迭代中的一种,相比python for循环速度会快很多. e ...
- 【转】GOOGLE-PROTOBUF与FLATBUFFERS数据的序列化和反序列化
转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/unity3d-game/1607.html 关于Protobuf 通过本文的转载和分享的相关链接,足够 ...
- Hibernate domain对象说明
一个domain对象对应于数据库的一张表(也可以表示出表关系) domain对象必须带一个无参构造函数 建议有一个无意义id,作为主键 建议非final,否则无法使用Hibernate的高级特性(懒加 ...
- 判断腾讯QQ是否在线
http://webpresence.qq.com/getonline?Type=1&1617052138: 判断腾讯QQ是否在线接口. 下面是个简单的例子: <!doctype htm ...
- flask-sqlalchemy 用法总结
Flask-SQLAlchemy是一个Flask扩展,能够支持多种数据库后台,我们可以不需要关心SQL的处理细节,操作数据库,一个基本关系对应一个类,而一个实体对应类的实例对象.Flask是一个轻量级 ...