LeetCode题解之Clone Graph
1、题目描述

2、问题分析
要遍历图,然后标记没有被复制的节点。
3、代码
class Solution {
private:
unordered_map<Node*, Node*> m;
public:
Node* cloneGraph(Node* node) {
if (node == NULL)
return NULL;
Node *copy = new Node(node->val, {});
queue<Node*> q;
m[node] = copy;
q.push(node);
while (!q.empty()) {
Node *cur = q.front();
q.pop();
for(Node* neighbor : cur->neighbors) {
if (m.find(neighbor) == m.end()) {
m[neighbor] = new Node(neighbor->val, {});
q.push(neighbor);
}
m[cur]->neighbors.push_back(m[neighbor]);
}
}
return copy;
}
};
LeetCode题解之Clone Graph的更多相关文章
- 【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 ...
- 【LEETCODE OJ】Clone Graph
Problem link: http://oj.leetcode.com/problems/clone-graph/ This problem is very similar to "Cop ...
- 【LeetCode】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【leetcode】133. Clone Graph
题目如下: Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph con ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- LeetCode: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
随机推荐
- Android权限大全(链接地址整理)
版权声明:本文为博主原创文章,未经博主允许不得转载. Manifest.permission https://developer.android.google.cn/reference/android ...
- centos 7 linux 安装与卸载 tomcat 7
一.声明 本文采用操作系统版本: Centos 7 Linux系统 版本源:CentOS-7-x86_64-DVD-1708.iso 官网下载地址:http://isoredirect.centos. ...
- SpringCloud入门之YAML格式文件规范学习
1. 认识 YAML YAML(发音 /ˈjæməl/)是一个类似 XML.JSON 的数据序列化语言.其强调以数据为中心,旨在方便人类使用:并且适用于日常常见任务的现代编程语言.因而 YAML 本身 ...
- Spring Boot (四)模板引擎Thymeleaf集成
一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...
- for 循环 和 Array 数组对象
博客地址:https://ainyi.com/12 for 循环 和 Array 数组对象方法 for for-in for-of forEach效率比较 - 四种循环,遍历长度为 1000000 的 ...
- 南大算法设计与分析课程复习笔记(4)L4 - QuickSort
一.快速排序 算法导论上关于快速排序有两种写法 第一种,从头到尾遍历,不断将小于基准元素的项移到前面.代码很简介,只需要维护一个交换位置,表示小于基准元素的末尾位置加一 我们看算法导论上的一个例子: ...
- .NET CORE 实践(3)--Visual Studio 2015 Update 3更新之后DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe无法正确安装
打开 https://www.microsoft.com/net/core#windows,点击 https://go.microsoft.com/fwlink/?LinkId=691129下载vs2 ...
- [转]Node.JS使用Sequelize操作MySQL
Sequelize官方文档 https://sequelize.readthedocs.io/en/latest/ 本文转自:https://www.jianshu.com/p/797e10fe23 ...
- 第一册:lesson twentynine..
原文:Come in ,Amy. A:Come in B. Shut the door,please. This bedroom's very untidy. B:What must I do Mrs ...
- CentOS 7.6安装桌面
# yum -y groups install "GNOME Desktop" # startx