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. NserviceBus+rabbitmq

    Ok so I figured this out after looking a bit at the code and the requirements for amqp URI and it sh ...

  2. silverlight简单数据绑定2

    <Grid x:Name="LayoutRoot" Background="white" Loaded="LayoutRoot_Loaded&q ...

  3. 扫地雷II

    感谢格致杭业晟同学改进完善 uses crt;var  i,j,k,ls,x,y:byte;  b:array[0..11,0..11] of shortint;  f:array[0..11,0.. ...

  4. 【知识点】业务连接服务(BCS)认证概念整理

    业务连接服务(BCS)认证概念整理 I. BDC认证模型 BDC服务支持两种认证模型:信任的子系统,模拟和代理. 在信任的子系统模型中,中间层(通常是Web服务器)通过一个固定的身份来向后端服务器取得 ...

  5. Lua 5.1 参考手册

    Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 云风 译 www.codingno ...

  6. 用wireshark抓包分析TCP三次握手、四次挥手以及TCP实现可靠传输的机制

    关于TCP三次握手和四次挥手大家都在<计算机网络>课程里学过,还记得当时高超老师耐心地讲解.大学里我遇到的最好的老师大概就是这位了,虽然他只给我讲过<java程序设计>和< ...

  7. HDU2045

    http://acm.hdu.edu.cn/showproblem.php?pid=2045 如果n-1的颜色和1相同,那么n有两种走法,如果n-1 的颜色和1不同,那么n只有1种选择方法 公式就是f ...

  8. 使用opencv设置图像的格式以及帧率

    最近楼主正在写一个关于图像存储的程序,LZ有一颗求知心,想要了解保存的图像的格式以及获取摄像头帧率.晚些时候会写一篇关于opencv获取摄像头并且保存每帧图像信息方法. 1.修改图像的像素显示: 首先 ...

  9. QueryRunner(common-dbutils.jar)

    QueryRunner update方法:* int update(String sql, Object... params) --> 可执行增.删.改语句* int update(Connec ...

  10. 【MySQL】MySQL 5.7 sys Schema

    sys库说明:http://dev.mysql.com/doc/refman/5.7/en/sys-schema-usage.html sys库使用说明:http://dev.mysql.com/do ...