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

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. 1
  2. / \
  3. / \
  4. 0 --- 2
  5. / \
  6. \_/

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

  1. /**
  2. * Definition for undirected graph.
  3. * struct UndirectedGraphNode {
  4. * int label;
  5. * vector<UndirectedGraphNode *> neighbors;
  6. * UndirectedGraphNode(int x) : label(x) {};
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
  12. // IMPORTANT: Please reset any member data you declared, as
  13. // the same Solution instance will be reused for each test case.
  14. typedef unordered_map<int, UndirectedGraphNode *> Map;
  15. if(node == NULL)return NULL;
  16. Map gmap;//保存克隆图的节点的地址,顺便作为节点是否访问的标记
  17. stack<UndirectedGraphNode *>gstack;
  18. UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
  19. gmap.insert(Map::value_type(res->label, res));
  20. gstack.push(node);
  21. while(gstack.empty() == false)
  22. {
  23. UndirectedGraphNode *p = gstack.top(), *newp;
  24. gstack.pop();
  25. if(gmap.find(p->label) != gmap.end())//查找克隆图节点是否已经构造
  26. newp = gmap[p->label];
  27. else
  28. {
  29. newp = new UndirectedGraphNode(p->label);
  30. gmap.insert(Map::value_type(p->label, newp));
  31. }
  32. for(int i = ; i < p->neighbors.size(); i++)
  33. {
  34. UndirectedGraphNode *tmp = p->neighbors[i];
  35. if(gmap.find(tmp->label) == gmap.end())
  36. {
  37. gmap.insert(Map::value_type(tmp->label,
  38. new UndirectedGraphNode(tmp->label)));
  39. gstack.push(tmp);
  40. }
  41. //设置克隆图节点的邻接点
  42. newp->neighbors.push_back(gmap[tmp->label]);
  43. }
  44. }
  45. return res;
  46. }
  47. };

【版权声明】转载请注明出处: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. Jmeter调试工具---Debug Sampler

    一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...

  2. 为Asp.net MVC中的RenderSection设置默认内容

    1. RenderSection的简单介绍 Asp.net MVC中提供了RenderSection方法,这样就能够在Layout中定义一些区块,这些区块留给使用Layout的view来实现比如我们定 ...

  3. SpringMVC 返回 html 视图页面,SpringMVC与Servlet,Servlet重定向与转发

    1. SpringMVC与Servlet的关系 SpringMVC框架是建立在Servlet之上的,提供各种功能,各种封装,各种方便的同时,它一点儿也没有限制Servlet,我们完全可以在Spring ...

  4. VISUAL STUDIO 调试

    调试术语 Visual Studio调试之断点基础篇 Visual Studio调试之断点进阶篇 不能设置断点的检查步骤 Visual Studio调试之断点技巧篇 Visual Studio调试之断 ...

  5. oracle树操作(select start with connect by prior)

    oracle中的递归查询可以使用:select .. start with .. connect by .. prior 下面将会讲述oracle中树形查询的常用方式,只涉及到一张表. 一. 建表语句 ...

  6. avahi-daemon启动失败-解决方法-linux

         avahi-daemon是一种Linux操作系统上运行在客户机上实施查找基于网络的Zeroconf service的服务守护进程. 该服务可以为Zeroconf网络实现DNS服务发现及DNS ...

  7. DataGridView合并单元格

    昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,然后直接使用e.Graphics.FillRectangle(backColorBru ...

  8. [转帖]迅为4412开发板最小linux系统的存储空间修改

    本文转自迅为论坛:http://www.topeetboard.com 最小linux系统的存储空间修改以修改成 1G 存储空间为例来修改,如果需要改成其他大小的存储空间,参照此方法修改即可. 首先连 ...

  9. nopcommerce3.3简洁版

    从nopcommerce里面分离出了基础框架,包括了用户.新闻.单页面.投票等模块,可以作为快速开发asp.net mvc项目的方案,有兴趣的朋友可以下载看看,由于时间仓促可能会有一些多余的文件没有清 ...

  10. java 字节流和字符流的区别 转载

    转载自:http://blog.csdn.net/cynhafa/article/details/6882061 java 字节流和字符流的区别 字节流与和字符流的使用非常相似,两者除了操作代码上的不 ...