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 ...
随机推荐
- yii2解析非x-www-form-urlencoded类型的请求数据(json,xml)
组件配置添加: 'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', 'applicat ...
- 一款兼容IE6并带有多图横向滚动的jquery特效
一款兼容IE6并带有多图横向滚动的jquery特效,自动切换多个图片的jquery特效效果, 为大家分享这个的原因是,这款特效在兼容IE6上面很完美,实用性就广很多了. 适用浏览器:IE6.IE7.I ...
- C语言-sizeof()与strlen()的区别【转】
先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...
- delphi控件安装与删除
附带通用控件安装方法:----------基本安装1.对于单个控件,Componet-->install component..-->PAS或DCU文件-->install;2.对于 ...
- 为啥 Objective-C 使用中括号来调用类方法?
原因在这篇文章中:http://stackoverflow.com/questions/23723838/why-does-objective-c-use-square-brackets-for-me ...
- 网络开发库从libuv说到epoll
引言 这篇博文可能有点水,主要将自己libuv的学习过程和理解. 简单谈方法. 有点杂. 那我们开始吧. 首先介绍 githup . 这个工具特别好用. 代码托管. 如果不FQ可能有点卡. 但是应该试 ...
- hdu 1008
题目意思是:给你N个数字 每个数字表示多少层楼 现在要你从0层楼开始坐电梯 一次按顺序走过这些楼层 规则是 上楼6秒 ,下楼4秒,每次到达一个楼层停5秒..... 思路:模拟 代码如下:(要注意 ...
- golang的nil
golang中什么样的类型可以赋值nil? 类型文档中有注定"uninitialized value is nil"的类型都可以赋值nil. golang的基本类型不能赋值nil: ...
- swift 命令行工具初探
亲爱的同学们好,今天我们要介绍这么一个东西.相信有过解释型语言(PHP,Ruby,等)使用经验的同学会更加熟悉,就是 Swift 也为我们提供了命令行运行工具,俗称 REPL.好了,我们进入正题,在安 ...
- IOS内存管理「3」- 自动释放的基本概念和用法