题目如下:实现克隆图的算法  题目链接

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

OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
/ \
/ \
0 --- 2
/ \
\_/

分析:BFS或者DFS遍历图,遍历的过程中复制节点,用哈希表保存新建立的节点的地址(同时这个哈希表可以用做节点访问标志)代码如下:                                                 本文地址

 /**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
typedef unordered_map<int, UndirectedGraphNode *> Map;
if(node == NULL)return NULL;
Map gmap;//保存克隆图的节点的地址,顺便作为节点是否访问的标记
stack<UndirectedGraphNode *>gstack;
UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
gmap.insert(Map::value_type(res->label, res));
gstack.push(node);
while(gstack.empty() == false)
{
UndirectedGraphNode *p = gstack.top(), *newp;
gstack.pop();
if(gmap.find(p->label) != gmap.end())//查找克隆图节点是否已经构造
newp = gmap[p->label];
else
{
newp = new UndirectedGraphNode(p->label);
gmap.insert(Map::value_type(p->label, newp));
}
for(int i = ; i < p->neighbors.size(); i++)
{
UndirectedGraphNode *tmp = p->neighbors[i];
if(gmap.find(tmp->label) == gmap.end())
{
gmap.insert(Map::value_type(tmp->label,
new UndirectedGraphNode(tmp->label)));
gstack.push(tmp);
}
//设置克隆图节点的邻接点
newp->neighbors.push_back(gmap[tmp->label]);
}
}
return res;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3418412.html

LeetCode:Clone Graph的更多相关文章

  1. LeetCode: Clone Graph 解题报告

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

  2. [LeetCode] Clone Graph 无向图的复制

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

  3. [LeetCode] Clone Graph 克隆无向图

    Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...

  4. [leetcode]Clone Graph @ Python

    原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...

  5. [Leetcode Week3]Clone Graph

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

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

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

  7. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  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. 21. Clone Graph

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

随机推荐

  1. python简单的爬虫,网页图片

    1 #!/usr/bin/python 2 #coding=utf-8 3 import urllib 4 import re 5 6 def gethtml(url): 7 page=urllib. ...

  2. Effective Java 23 Don't use raw types in new code

    Generic types advantage Parameterized type can provide erroneous check in compile time. // Parameter ...

  3. for循环练习题

    ■■■■■■■■■■■■■■■■■■■■■■■■■ 代码: <script> for(i=0;i<5;i++) { for(j=0;j<5;j++) { document.wr ...

  4. java使用this关键字调用本类重载构造器

    在构造器中可以调用本类的其他重载构造器,不能使用构造器名称来调用另一个构造器,而是应该使用Java特定的this(-.)来调用. this(-.)方法必须出现在构造器中的第一行,用来调用其他重载构造器 ...

  5. linux配置文件的一些调优

    Linux中所有东西都是文件,一个socket就对应着一个文件描述符,因此系统配置的最大打开文件数以及单个进程能够打开的最大文件数就决定了socket的数目上限:但是linux是有文件句柄限制的,而且 ...

  6. 在Myeclipse中添加User Library,用户自己的库

    在Myeclipse中添加User Library,用户自己的库 作用:可以将常用的jar包添加到一个固定的库中,避免每一次都要手动导入. 步骤: 1.选择项目,点击Myeclipse的window菜 ...

  7. 一个不错的shell 脚本教程 入门级

    一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂     建立一个脚本 Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行s ...

  8. 数据结构--树状数组(黑龙江省第八届大学生程序设计竞赛--post office)

    例题来源: 题目: 1468: Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered fr ...

  9. Android监听键盘显示和隐藏

    问题概况:横板cocos2dx游戏,点击输入框弹出键盘时,界面要求跟随网上平易,不能挡住输入框.这种问题只出现在非全屏键盘到情况下. 方案1:mainActivity重写onconfiguration ...

  10. Tomcat 内存和线程配置优化

    1. tomcat 的线程配置参数详情如下: 修改conf/server.xml中的<Connector .../> 节点如下: <Connector port="8080 ...