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 ...
随机推荐
- (转)AVI文件格式解析+AVI文件解析工具
AVI文件解析工具下载地址:http://download.csdn.net/detail/zjq634359531/7556659 AVI(Audio Video Interleaved的缩写)是一 ...
- DDL DML DCL语句
总体解释:DML(data manipulation language):自动提交的数据库操作语言 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样 DDL( ...
- eclipse 清楚git记录的密码
菜单:window->preferences弹出上述对话框
- [vB.NET]为控件添加鼠标悬浮时的提示气泡
实例代码: Dim k As ToolTip k = New ToolTip() k.AutoPopDelay = '显示出气泡后的延时时间(毫秒) k.InitialDelay = '出现前的延时( ...
- git 相关
github入门 一.先了解 相比CVS\SVN优势: - 支持离线开发,离线Repository- 强大的分支功能,适合多个独立开发者协作- 速度快 github 本地有仓库,储存着所有repo ...
- 学习了一下javascript的模块化编程
现在在我脑海里关于“模块化”的概念是这些词:简单.具有逻辑之美.易用.健壮.可扩展.似乎这些形容与我现在水平写出的代码有点格格不入啊. 所以今天想了解和简单的实践一下“模块化开发”. 1.首先学习一下 ...
- Linux下的GNU Emacs 24命令_信息竞赛使用_C++
C代表Ctrl,M代表Alt 一.文件命令 C-x b 新建 build C-x C-f 打开文件 find C-s 保存文件 save C-x C-w 另存为 为wei w C-x C-b 打开所有 ...
- 【练习】移动数据----infile *
要求: ①指定bad文件: ②挂在之前将目标表delete: ③导入的的数据在控制文件中. 1.创建目录对象: :: SYS@ORA11GR2>create or replace directo ...
- '<', hexadecimal value 0x3C, is an invalid 问题解决
你的web.config 里面一定有个节点的不完整,如 错误如下: 正确的如下:
- 纸上谈兵:队列(queue)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 队列(queue)是一个简单而常见的数据结构.队列也是有序的元素集合.队列最大的特 ...