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

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. Effective Java 43 Return empty arrays or collections, not nulls

    Feature Return empty arrays or collection Return nulls Avoids the expense of allocating the array N ...

  2. cocos2d-x之value

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...

  3. 闭包Closures

    所谓闭包,可以理解为一个可以用于函数,参数,返回值处的代码块 import Foundation func isGood(a:Int,b:Int)->Bool{ return a>b; } ...

  4. JavaScript吸顶灯的实现

    吸顶灯是各站点常用的一个功能,它有两个特性 向下滚动到div位置时,该div一直固定在页面的顶部 向上滚动到div原有位置时,div又恢复到文档中的原位置 div可能是一个“分类菜单”,也可能是一个“ ...

  5. Linux系统之压缩、解压缩,vi编辑器,系统初始化服务和系统监控

    一.正文处理,压缩与解压缩 1.内容重定向>与>> >:覆盖,将>号左边的结果覆盖到>号右边的文件中,如果文件不存在,则先创建一个新的空文件并覆盖 >> ...

  6. Reading WebSites

    oracle http://www.eygle.com/archives/2006/02/the_sun_repays_industriously.html 蕃茄土豆: https://pomotod ...

  7. [转]用NPOI操作EXCEL--数据有效性

    本文转自:http://www.cnblogs.com/atao/archive/2009/09/22/1572170.html 在有些情况下(比如Excel引入),我们可能不允许用户在Excel随意 ...

  8. 【ASP.NET 进阶】无刷新上传图片之一:利用一般处理程序

    效果图: 源代码地址:https://github.com/YeXiaoChao/UploadThePic

  9. 2016.6.12 codevs搜索练习

    1.codevs 3143 二叉树的序遍历 /*只要把输出根节点的位置调换一下就可以了*/ #include<iostream> using namespace std; #include ...

  10. 边工作边刷题:70天一遍leetcode: day 85

    Find the Celebrity 要点: 这题从solution反过来想比较好:loop through n同时maintain一个candidate:如果cand认识某个i,那么modify c ...