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. Socket 连接"由于目标机器积极拒绝,无法连接" 的诊断

    1.如果是采用TCP/udp协议进行连接,检查windows防火墙是否开放相应SocketTCP/udp端口;   简单的检测方法是关闭windows防火墙后再试;2.如果服务器端和客户端均在本机上运 ...

  2. Fighting Game

    感谢上外静中任淳同学提供   uses crt; label   h;         //h是重新开始游戏 const   y1=18;   y2=18;       //p1,p2的纵坐标 var ...

  3. 剑指Offer:面试题24——二叉搜索树的后序遍历序列(java实现)

    问题描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 思路: 1.首先后序遍历的结果是[(左子 ...

  4. C++ 删除字符串的两种实现方式

    C++实现删除给定字符串的给定字符串思路主要有这么几种实现方式: 1.KMP算法2.用STL的string的 find,然后用erase3.用C的strstr找到字串位置,然后用strncpy写到新串 ...

  5. linux mono环境

    安装好 CentOS 6.5 之后 1.更新系统 在命令行下执行 yum –y update 2.安装必要的软件 yum -y install gcc gcc-c++ bison pkgconfig ...

  6. ubuntu 13.04 lighttped mysql php

    apt-get update sudo apt-get install lighttpd php5-cgi Enable the fastcgi module and the php configur ...

  7. 深入理解JS异步编程(一)

    js事件概念 异步回调 首先了讲讲js中 两个方法 setTimeout()和 setInterval() 定义和用法: setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 ...

  8. UICollectionView布局功能

    UIConllectionView和UITableView类似,也是展示数据,但不同于UITableView那种规则的布局,UICollectionView可以实现不规则的布局,即瀑布流. 创建UIC ...

  9. i-doit

        官网:http://www.i-doit.org/,有免费版和专业版. 开源:http://sourceforge.net/projects/i-doit/ › Features CMDB I ...

  10. 用BlendFunc实现舞台灯光和刮刮卡效果

    [转]http://code.lovemiao.com/?p=136#more-136 之前写过一篇<不规则形状按钮的点击判定>,利用了CCRenderTexture创建一块画布,可以在上 ...