LeetCode OJ-- Clone Graph **@
https://oj.leetcode.com/problems/clone-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 node; vector<UndirectedGraphNode *> allNode;
unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> oldToNew; // 对于每一个node,都建立一个新node,把这关系存到map里面。之后对每个旧node,添加相应新node里的neighbour allNode.push_back(node);
UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
oldToNew.insert(make_pair(node,newNode)); int index = ;
UndirectedGraphNode *current;
UndirectedGraphNode *neighNode;
vector<UndirectedGraphNode *> neighbours;
// build new nodes
while(index < allNode.size())
{
current = allNode[index];
index++;
neighbours = current->neighbors; for(int i = ; i < neighbours.size(); i++)
{
neighNode = neighbours[i];
// 之前没有添加过它相应的
if(oldToNew.find(neighNode) == oldToNew.end())
{
UndirectedGraphNode *newNode = new UndirectedGraphNode(neighNode->label);
oldToNew.insert(make_pair(neighNode,newNode)); allNode.push_back(neighNode);
}
}
}
// build neighbours
unordered_map<UndirectedGraphNode *, UndirectedGraphNode *>::iterator itr;
for(itr = oldToNew.begin(); itr!=oldToNew.end(); itr++)
{
current = itr->first;
neighbours = current->neighbors;
for(int i = ; i < neighbours.size(); i++)
{
itr->second->neighbors.push_back(oldToNew[neighbours[i]]);
}
}
return oldToNew[node];
}
};
LeetCode OJ-- Clone Graph **@的更多相关文章
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- [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】Clone Graph(python)
类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...
- [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】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 ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
随机推荐
- [DFNews] 入侵汽车控制刹车和油门?——速度与激情6 的节奏?
原文跳转: http://arstechnica.com/security/2013/07/disabling-a-cars-brakes-and-speed-by-hacking-its-compu ...
- MVC 项目中为什么会有两个web.config
我们对MVC 并不陌生, 在创建MVC项目时,总会发现,在工程目录 中有两个 web.config 文件,一个是在工程的根目录下,一是在 views 下,两个web.config 中的内容也不尽相同, ...
- express html模板项目搭建
初学express的亲们,估计要弄ejs和jade会比较烦躁,那就先html开始,简单笔记如下: 1.新建项目文件夹demotest 2.进入demotest >express -e ...
- 2.擦除开发板iNand中的uboot的方法
(1)在linux和android系统下,擦除uboot的方法: busybox dd if=/dev/zero of=/dev/block/mmcblk0 bs=512 seek=1 c ...
- spring2.0包说明【转】
Spring压缩包目录说明 关键字: sring jar 1. Spring压缩包目录说明 aspectj目录下是在Spring框架下使用aspectj的源代码和测试程序文件. Aspectj是jav ...
- ThinkPHP 学习记录
index.php //入口文件 define('APP_DEBUG',True); //开启调试模式 define('APP_PATH','./Application/'); //定义应用目录 re ...
- Spring项目解决Post乱码
Java EE解决Post乱码:在web.xml中加入: <filter> <filter-name>encodingFilter</filter-name> &l ...
- Bootstrap 更改Navbar默认样式
alt+shift+n 新建文件ctrl+shift+/ 注释ctrl+shift+f 重新排版代码ctrl+/ 注释 /* navbar */.navbar-default { background ...
- FizzlerEx —— 另一个HtmlAgilityPack的CSS选择器扩展,
之前我介绍过HtmlAgilityPack的CSS选择器扩展——ScrapySharp,它可以非常方便的实现通过CSS选择器表达式来查询HtmlNode.今天在使用的过程中,发现它不支持nth-chi ...
- 线程池的使用ExecutorService
private ExecutorService executorService = Executors.newFixedThreadPool(5); // 引入线程池来管理多线程 private vo ...