Clone Graph问题及解法
问题描述:
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 #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(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问题及解法相关推荐
- 133. Clone Graph
欢迎fork and star:Nowcoder-Repository-github 133. Clone Graph 题目 Clone an undirected graph. Each node ...
- 【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph
LeetCode 133. Clone Graph Solution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html ...
- Clone Graph
题目 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. O ...
- [leetcod] Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- leetcode -- Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. [解题思 ...
- LeetCode Clone Graph(搜索问题)
问题:给出一个邻接列表表示的图,要求克隆这个图 思路: 1.深度优先搜索,使用map记录结点与其克隆结点之间的映射关系 .当遍历结点时,如果在map中存在结点的克隆,则直接返回.否则创建结点的克隆,加 ...
- 133. Clone Graph 克隆图
给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node {public in ...
- LeetCode刷题之python解法(持续更新)
1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...
- 【LeetCode】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 1 /* 2 // Definition for a Node. 3 ...
最新文章
- 面试官问:高并发下,你都怎么选择最优的线程数?
- SH-20403AXIS12双轴蓝牙移动框架
- oracle spatial 数据导入_【转】 Oracle Spatial 基本操作
- 三维动画制作的基本流程
- 算法导论学习笔记 第6章 堆排序
- LeetCode 第 35 场双周赛(216/2839,前7.61%)
- Eclipse手动配置svn
- OpenCV人脸识别之二:模型训练
- 分享一本Swift好书
- vue通过webpack打包后怎么运行
- 【Java 强化】代码规范、JavaBean、lombok、内省(Introspector)、commons 项目、注解详解
- 【原创】MySQL 模拟PostgreSQL generate_series 表函数
- Springboot 内嵌 Tomcat 版本查看
- dcs world f15c教学_陕西【精细化工dcs控制】施工
- 日常知识点之公开课内存碎片优化(内存池)
- python漏洞扫描器编写,用Python编写Web漏洞检测工具
- 【基础常识】什么是字符以及字符串的定义
- mysql容器保存为镜像实战操作(拷贝方法)
- 中文单栏latex模板
- 华为鸿蒙系统能玩安卓游戏吗_华为鸿蒙系统出来后安卓游戏账号还能用吗 鸿蒙和安卓游戏数据会互通吗...