Clone Graph [LeetCode]
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
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 #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
Summary: BFS, one queue for BFS, one map for visited nodes, one map for mapping between original nodes and the nodes of new graph.
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node == NULL)
return NULL;
vector<UndirectedGraphNode *> node_queue;
map<UndirectedGraphNode *, bool> visited;
map<UndirectedGraphNode *, UndirectedGraphNode *> node_map;
UndirectedGraphNode * root = new UndirectedGraphNode(node->label);
node_map[node] = root;
visited[node] = true;
node_queue.push_back(node); while(node_queue.size() > ){
UndirectedGraphNode * node = node_queue[];
node_queue.erase(node_queue.begin());
UndirectedGraphNode * new_node = node_map[node];
for(auto item : node->neighbors){
if(visited.find(item) != visited.end()){
new_node->neighbors.push_back(node_map[item]);
}else{
node_queue.push_back(item);
UndirectedGraphNode * new_item = new UndirectedGraphNode(item->label);
node_map[item] = new_item;
new_node->neighbors.push_back(new_item);
visited[item] = true;
}
}
} return root;
}
};
Clone Graph [LeetCode]的更多相关文章
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- Clone Graph——LeetCode
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- 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 ...
- LeetCode: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
随机推荐
- FreeSWITCH第三方库(音频)的简单介绍(一)
FreeSWITCH使用了大量的第三方库,本文档主要介绍音频相关库的信息: 视频相关库的信息介绍参考:http://www.cnblogs.com/yoyotl/p/5488890.html 其他相关 ...
- ImageMagick远程命令执行工具检测工具
ImageMagick这个漏洞昨天晚上就出来了,今天才有时间研究一下,今天自己也测试了一下 效果图: ======================= 用lua写了一个检测脚本 print (" ...
- win8和ubuntu双系统安装
做了一个windows和Ubuntu双系统,参考了一些文章.网上资料不少,我就不重复了. 虽然没什么难度,但是有些细节在装的时候需要注意.不然造成资料丢失,系统崩溃,你就得不偿失,需要折腾花费较长的时 ...
- 高效使用Vector
参考网页: http://www.cnblogs.com/biyeymyhjob/archive/2013/05/11/3072893.html#undefined 1.初始化的时候,最好先用rese ...
- Spring的DI(Ioc) - 注入集合类型
1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...
- iOS - UINavigationController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController @available(iOS 2 ...
- QQReg.java
import java.awt.*; import javax.swing.*; public class QQReg extends JFrame{ public static void main( ...
- 使用poi读写Excel
对于一个程序员来说,文件操作是经常遇到的,尤其是对Excel文件的操作. 在这里介绍一下我在项目中用到的一个操作Excel的工具——POI.关于POI的一些概念,网络上很多,详细信息大家可以自行百度, ...
- 浅谈 MVP in Android
一.概述 对于MVP(Model View Presenter),大多数人都能说出一二:“MVC的演化版本”,“让Model和View完全解耦”等等.本篇博文仅是为了做下记录,提出一些自己的看法,和帮 ...
- OpenGL的gluPerspective和gluLookAt的关系[转]
函数原型void gluLookAt(GLdoble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, ...