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

  1. [Leetcode Week3]Clone Graph

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

  2. [LeetCode] 133. Clone Graph 克隆无向图

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

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

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

  5. 【leetcode】Clone Graph(python)

    类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...

  6. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  7. Leetcode#133 Clone Graph

    原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...

  8. 【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 ...

  9. LeetCode: Clone Graph 解题报告

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

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

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

随机推荐

  1. 常见面试问题 - Useful Links

    1. Data Structure & Algorithm - 二叉树 http://baike.baidu.com/link?url=jKNdOOipbp-gloTVmSU4PT2mVB94 ...

  2. Window中常见的dos命令

    1.如何实行操作dos命令:如果是Windows电脑,从开始--->所有程序---->附件--->命令提示 这样就可以开始命令提示符了 2关于一些dos命令: 2.1 盘符切换:盘符 ...

  3. python模块概况,json/pickle,time/datetime,logging

    参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html http://www.cnblogs.com/alex3714/articles/51 ...

  4. projecteuler 10001st prime (求出第10001个质数)

    By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. ...

  5. Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构

    本文提供了一个在Windows环境下使用Visual Studio 2013编辑现有源代码并且保持目录结构的方法.本文使用VS2013中文社区版做示例(本版本为免费版,可在VS官网下载),其他版本的V ...

  6. 关于DCOM的安全性

    关于DCOM的安全性 DCOM的安全性设置在注册表中. 2. 通过DCOMCNF.exe可以配置

  7. A potentially dangerous Request.Form value was detected from the client

    提交表单中包含特殊字符如<script>可能被认为是跨站攻击代码:解决方法很多,如stackoverflow上的web.config中加设置的方法不中肯[如原贴中Jamie M所说],主要 ...

  8. CUnit的用法

    转自:http://blog.csdn.net/scucj/article/details/4385630/ CUnit下载地址: http://sourceforge.net/projects/cu ...

  9. 实践一:Linux基础实践

    一.Linux基础实践 1.1 1. 掌握软件源的维护方法,配置系统使用软件源镜像.掌握通过软件源来查找,安装,卸载,更新软件的方法. 这部分内容在许多学长学姐的报告里都有很详细的讲解,我在此就不赘述 ...

  10. Rule of Modularity

    As Brian Kernighan once observed, “Controlling complexity is the essence of computer programming.” . ...