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的更多相关文章

  1. 【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 ...

  2. 【LEETCODE OJ】Clone Graph

    Problem link: http://oj.leetcode.com/problems/clone-graph/ This problem is very similar to "Cop ...

  3. 【LeetCode】133. Clone Graph 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. 【leetcode】133. Clone Graph

    题目如下: Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph con ...

  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: Clone Graph 解题报告

    Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...

  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. java字符流操作flush()方法及其注意事项

    java字符流操作flush()方法及其注意事项   flush()方法介绍 查阅文档可以发现,IO流中每一个类都实现了Closeable接口,它们进行资源操作之后都需要执行close()方法将流关闭 ...

  2. 简易远程消息交换协议SRMP

    一.SRMP目标定位 经过十多年实战经验积累以及多方共同讨论,新生命团队(https://github.com/newlifex)制订了一种简单而又具有较好扩展性的RPC(Remote Procedu ...

  3. Docker中运行nginx并挂载本地目录到镜像中

    1.1 从hup上pull镜像1.2 创建将要挂载的目录1.3 先要有配置文件才能启动容器1.3.1 vim /data/nginx/conf/nginx.conf1.3.2 vim /data/ng ...

  4. spring-boot(八) springboot整合shiro-登录认证和权限管理

    学习文章:springboot(十四):springboot整合shiro-登录认证和权限管理 Apache Shiro What is Apache Shiro? Apache Shiro是一个功能 ...

  5. 【leet-code】542. 01 矩阵

    题目描述 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...

  6. MySQL的binlog恢复(Windows下)

    前言 在最近的工作中,由于自己粗(zuo)心(si)误update操作导致几百行的数据出现错误,在心急如焚的同时(那时候我竟然不知道除了备份之后还有binlog日志恢复)立马查资料学习binlog的恢 ...

  7. WebSocket+Java 私聊、群聊实例

    前言 之前写毕业设计的时候就想加上聊天系统,当时已经用ajax长轮询实现了一个(还不懂什么是轮询机制的,猛戳这里:https://www.cnblogs.com/hoojo/p/longPolling ...

  8. 关于我空间那篇名为《JavaScript axError:Unexpected token ILLEGAL 很简单的代码……》的随笔

    如图所示,我是空间的那片随笔,这里就不贴出链接了,也不希望你有这个兴趣,不知道会不会有人无意间闯入,额,自从日志用windows live writer 2009 发了那篇随笔后,我在网页上一直没能正 ...

  9. ubuntu 16.04服务器 搭建php LAMP环境

    1.安装Apache 下载目录: sudo apt update 安装程序: sudo apt install apache2 检查是否安装成功: sudo systemctl status apac ...

  10. 微信小程序实现支付功能

    小程序支付,没有封装支付代码:直接上一段可用的流程代码吧:微信小程序支付官网文档有详细的说明,这里我就不再赘述啦:客户端js: wx.request({ url:'https://www.xxxx.c ...