Leetcode#133 Clone Graph
方法I,DFS
一边遍历一边复制
借助辅助map保存已经复制好了的节点
对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟。
代码:
map<UndirectedGraphNode *, UndirectedGraphNode *> old2new;
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node)
return NULL;
if (old2new.find(node) != old2new.end())
return old2new[node];
UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(node, newNode));
for (auto n : node->neighbors)
newNode->neighbors.push_back(cloneGraph(n));
return newNode;
}
方法II,BFS
一边遍历一边复制
同样借助一个辅助map保存复制过的节点,还需要一个set保存已经遍历过的节点
代码:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node)
return NULL;
map<UndirectedGraphNode *, UndirectedGraphNode *> old2new;
unordered_set<UndirectedGraphNode *> copied;
queue<UndirectedGraphNode *> que;
old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(node, new UndirectedGraphNode(node->label)));
que.push(node);
while (!que.empty()) {
UndirectedGraphNode *front = que.front();
que.pop();
if (copied.find(front) != copied.end())
continue;
for (auto n : front->neighbors) {
if (old2new.find(n) == old2new.end()) {
UndirectedGraphNode *newNode = new UndirectedGraphNode(n->label);
old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(n, newNode));
que.push(n);
}
old2new[front]->neighbors.push_back(old2new[n]);
}
copied.insert(front);
}
return old2new[node];
}
Leetcode#133 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 ...
- 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 ...
- 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 ...
- 【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 Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- 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】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
随机推荐
- nodejs服务器anywhere简介
一句话:随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装 npm install anywhere -g 执行 $ anywhere // or with port $ anywhere ...
- PHP加密解密函数
<?php/***功能:对字符串进行加密处理*参数一:需要加密的内容*参数二:密钥*/function passport_encrypt($str,$key){ //加密函数 srand((do ...
- 点击后弧形展开的炫酷菜单--第三方开源-- CircularFloatingActionMenu(一)
CircularFloatingActionMenu在github上项目主页地址:https://github.com/oguzbilgener/CircularFloatingActionMenu ...
- WCF 内存入口检查失败
WCF 内存入口检查失败 Memory gates checking failed 异常信息:内存入口检查失败,因为可用内存(xxx 字节)少于总内存的 xx%.因此,该服务不可用于传入的请求.若 ...
- Html5 Canvas一个简单的画笔例子
相比了下Qt quick的canvas和HTML5的canvas,发现HTML5 Canvas在同样绘制绘制操作下性能比Qt的canvas强很多,附上一个HTML5 canvas画笔一例子 var D ...
- C语言中进制知识总结
1.什么是进制 进制是一种计数的方式,常用的有二进制.八进制.十进制.十六进制.任何数据在计算机内存中都是以二进制的形式存放的. 我对进制的个人理解,二进制数是以2为计算单元,满2进1位的数:八进制数 ...
- Java入门到精通——基础篇之面向对象
一.概述. Java属于面向对象的一种语言,因为Java是面向对象的语言所以这个语言的诞生需要有五个基本特性: 1)万物皆为对象. 2)程序是对象的集合. 3)每个对象都有自己的由其他对象所构成的存储 ...
- MySQL快速生产表的描述
# mysql快速生成表的描述 SELECT column_name AS `列名`, CONCAT_WS(' (', data_type, character_maximum_length) AS ...
- Drawable
今天简单的介绍一下有关以下5中的应用: Statelistdrawable Layerdrawable Shapeddrawable Clipdrawable Animationdrawable 1. ...
- bzoj 1798 [Ahoi2009]Seq 维护序列seq
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 线段树区间更新: 1. 区间同同时加上一个数 2. 区间同时乘以一个数 #inclu ...