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
/ \
\_/

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]的更多相关文章

  1. 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. ...

  2. Clone Graph——LeetCode

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  3. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  4. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  5. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  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: Clone Graph 解题报告

    Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...

  8. 21. Clone Graph

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  9. 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 ...

随机推荐

  1. MySQL(二) —— 数据类型与操作数据表

    数据类型 数据类型是指列.存储过程参数.表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型. 整型:TYNINT(-2^7 ~ 2^7-1); SMALLINT(-2^15 ~ ...

  2. tophat cufflinks cuffcompare cuffmerge 的使用

    Cole Trapnell said: there are three strategies: 1) merge bams and assemble in a single run of Cuffli ...

  3. Django serializers 序列化 rest_framework

    参考官方文档1(你懂的):http://www.django-rest-framework.org/api-guide/serializers/ 参考官方文档2(你懂的):http://www.dja ...

  4. linux下(修改|忘记)mysql密码

    好尴尬,经常忘记自己的密码 修改的用户都以root为列.一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p p ...

  5. emacs学习

    (set-default-font "Consolas-14")      // 设置字体及其大小 M-数字 命令 C-数字 命令

  6. SpringMVC 服务器端验证

    1.导入JSR303验证类库Jar包2.在MVC的配置文件中添加<mvc:annotation-driven/>的配置3.在MVC的配置文件中添加验证器的配置4.在接收表单数据的类中添加验 ...

  7. eclipse注释模板

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...

  8. nslookup基础用法

    简单介绍 简单介绍如下: 实例:查询163.com域名信息 D:>nslookup Default Server: ns-px.online.sh.cn Address: 202.96.209. ...

  9. 是时候改变你的开发方式了-XAF信息系统快速框架介绍

    我是一名.Net开发者,从DOS时代Turbo c 算起(1996年),马上满20年了.想想写过的代码真是不少,却做了很多重复反复的编码工作.当然中间也带过团队做过几个大项目,但是代码仍没写够,还是每 ...

  10. git学习笔记02-创建一个仓库提交一个文件-原来就是这么简单

    打开安装好的git bash,设置你的git信息  (这个随便写就行) 初始化一个Git仓库,分三步.(创建文件夹也可以手动创建,也可以命令行创建) 第一步,进到一个目录  cd e: 第二步,创建一 ...