问题描述:

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/ \\_/

问题分析:

典型的DFS问题。不多说明,直接上代码。

过程详见代码:

/*** 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 NULL;UndirectedGraphNode * newnode = new UndirectedGraphNode(node->label);unordered_map<int, UndirectedGraphNode *> labelmap;labelmap[node->label] = newnode;bl(node, newnode, labelmap);return newnode;}void bl(UndirectedGraphNode * node, UndirectedGraphNode * newnode, unordered_map<int, UndirectedGraphNode *>& labelmap){if (node == NULL) return;for (int i = 0; i < node->neighbors.size(); i++){int label = node->neighbors[i]->label;if (labelmap.count(label))newnode->neighbors.emplace_back(labelmap[label]);else{UndirectedGraphNode * n = new UndirectedGraphNode(label);newnode->neighbors.emplace_back(n);labelmap[label] = n;bl(node->neighbors[i], n, labelmap);}}}
};

Clone Graph问题及解法相关推荐

  1. 133. Clone Graph

    欢迎fork and star:Nowcoder-Repository-github 133. Clone Graph 题目 Clone an undirected graph. Each node ...

  2. 【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph

    LeetCode 133. Clone Graph Solution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html ...

  3. Clone Graph

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

  4. [leetcod] Clone Graph

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

  5. leetcode -- Clone Graph

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

  6. LeetCode Clone Graph(搜索问题)

    问题:给出一个邻接列表表示的图,要求克隆这个图 思路: 1.深度优先搜索,使用map记录结点与其克隆结点之间的映射关系 .当遍历结点时,如果在map中存在结点的克隆,则直接返回.否则创建结点的克隆,加 ...

  7. 133. Clone Graph 克隆图

    给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node {public in ...

  8. LeetCode刷题之python解法(持续更新)

    1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...

  9. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 1 /* 2 // Definition for a Node. 3 ...

最新文章

  1. 面试官问:高并发下,你都怎么选择最优的线程数?
  2. SH-20403AXIS12双轴蓝牙移动框架
  3. oracle spatial 数据导入_【转】 Oracle Spatial 基本操作
  4. 三维动画制作的基本流程
  5. 算法导论学习笔记 第6章 堆排序
  6. LeetCode 第 35 场双周赛(216/2839,前7.61%)
  7. Eclipse手动配置svn
  8. OpenCV人脸识别之二:模型训练
  9. 分享一本Swift好书
  10. vue通过webpack打包后怎么运行
  11. 【Java 强化】代码规范、JavaBean、lombok、内省(Introspector)、commons 项目、注解详解
  12. 【原创】MySQL 模拟PostgreSQL generate_series 表函数
  13. Springboot 内嵌 Tomcat 版本查看
  14. dcs world f15c教学_陕西【精细化工dcs控制】施工
  15. 日常知识点之公开课内存碎片优化(内存池)
  16. python漏洞扫描器编写,用Python编写Web漏洞检测工具
  17. 【基础常识】什么是字符以及字符串的定义
  18. mysql容器保存为镜像实战操作(拷贝方法)
  19. 中文单栏latex模板
  20. 华为鸿蒙系统能玩安卓游戏吗_华为鸿蒙系统出来后安卓游戏账号还能用吗 鸿蒙和安卓游戏数据会互通吗...

热门文章

  1. 盛大九年征程 盛大的娱乐帝国 盛大维稳
  2. PhpExcel 写一个漂亮的表格
  3. 【复盘】2022年度复盘
  4. DKIM、DMARC 和 SPF:设置电子邮件安全
  5. 网页图片加载优化方法总结
  6. 打造高效团队的四个着力点
  7. Qt的跨平台的部分原理和机制
  8. 小白学3D建模推荐3dsMax,这些功能你必须知道!
  9. Android View的坐标获取方法
  10. unity3D中的伽马空间和线性空间