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 ...
随机推荐
- mysql 开发进阶篇系列 19 MySQL Server(innodb_flush_log_at_trx_commit与sync_binlog)
一. innodb_flush_log_at_trx_commit 这个参数名称有个log,一看就是与日志有关.是指:用来控制缓冲区(log buffer)中的数据写入到日志文件(log file), ...
- Android利用Intent与其他应用交互
前言: 上一篇博客给大家聊了Intent的定义,分类.属性和功能,相信大家对于Intent在Android中的作用已经清楚,这一篇博客将会给大家聊Intent的用法. Android系统的一个重要特性 ...
- Chapter 4 Invitations——2
To my dismay, I found myself the center of attention for the rest of that week. 令我沮丧的是, 我发现我自己剩余注意力的 ...
- redis学习总结-redis作为MyBatis的自定义缓存
1.RedisCache.java package com.houtai.cache; import java.util.concurrent.locks.ReadWriteLock; import ...
- vue-11-路由嵌套-参数传递-路由高亮
1, 新建vue-router 项目 vue init webpack vue-router-test 是否创建路由: 是 2, 添加路由列表页 在 component下创建 NavList 页面 & ...
- Java网络编程的基本网络概念
前言 自己网络这方面的知识很是薄弱,每次面试被问到这部分都会卡壳,所以很尴尬,然后最近也是有些时间了,就赶紧把自己的不足补充一下.虽然最近也在看设计模式,但是总看设计模式也容易烦,所以就并行学习,看看 ...
- Perl和操作系统交互(一):system、exec和反引号
调用操作系统命令:system函数 system函数可以直接让perl调用操作系统中的命令并执行. system入门示例 例如: #!/usr/bin/perl system 'date +" ...
- tomcat编译项目后,classes文件没有相应的改变;
tomcat编译项目后,classes文件没有相应的改变: tomcat不能将项目部署到服务器: 1.首先,在tomcat安装目录webapps中将编译后的整个项目删掉,然后再在eclipse将tom ...
- 第一次:lesson eighty seven。
原文: A car crash. A:Is my car ready yet? B:I don't know sir,what's the number of your car? A:It's LFZ ...
- 【转载】 IIS服务器防盗链设置
在实际运行的服务器环境中,我们自己网站中的资源一般不希望被外部网站引用,被外部网站引用IIS网站中的资源文件,一是会加重了服务器的负担,二是占用了你自己服务器的外网带宽资源,因此我们希望防止盗链这种情 ...