原题地址

方法I,DFS

一边遍历一边复制

借助辅助map保存已经复制好了的节点

对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟。

代码:

 map<UndirectedGraphNode *, UndirectedGraphNode *> old2new;

 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node)
return NULL;
if (old2new.find(node) != old2new.end())
return old2new[node]; UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label);
old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(node, newNode));
for (auto n : node->neighbors)
newNode->neighbors.push_back(cloneGraph(n));
return newNode;
}

方法II,BFS

一边遍历一边复制

同样借助一个辅助map保存复制过的节点,还需要一个set保存已经遍历过的节点

代码:

 UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node)
return NULL; map<UndirectedGraphNode *, UndirectedGraphNode *> old2new;
unordered_set<UndirectedGraphNode *> copied;
queue<UndirectedGraphNode *> que; old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(node, new UndirectedGraphNode(node->label)));
que.push(node);
while (!que.empty()) {
UndirectedGraphNode *front = que.front();
que.pop();
if (copied.find(front) != copied.end())
continue;
for (auto n : front->neighbors) {
if (old2new.find(n) == old2new.end()) {
UndirectedGraphNode *newNode = new UndirectedGraphNode(n->label);
old2new.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(n, newNode));
que.push(n);
}
old2new[front]->neighbors.push_back(old2new[n]);
}
copied.insert(front);
} return old2new[node];
}

Leetcode#133 Clone Graph的更多相关文章

  1. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  2. leetcode 133. Clone Graph ----- java

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  3. Java for LeetCode 133 Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  4. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  5. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

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

  7. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  8. 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 ...

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

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

随机推荐

  1. linux iostat命令详解 磁盘操作监控工具

    Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视. 它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况. ...

  2. Android的ADT内容助手快捷方式设置

    请注明出处:http://www.cnblogs.com/killerlegend/p/3550019.html  Written By KillerLegend 先将Word Completion的 ...

  3. 13.python中的字典

    字典其实和之前的元祖和列表功能相似,都是用来储存一系列对象的.也就是一种可变容器,或者是我所比喻的革新派的菜单. 但也不是完全相同,我在之前曾经将字典称为特殊的'序列',是字典拥有序列的部分特性,但是 ...

  4. python的pip和virtualenv使用心得

    pip可以很方便的安装.卸载和管理Python的包.virtualenv则可以建立多个独立的虚拟环境,各个环境中拥有自己的python解释器和各自的package包,互不影响.pip和virtuale ...

  5. 界面控件 - 滚动条ScrollBar

    界面是人机交互的门户,对产品至关重要.在界面开发中只有想不到没有做不到的,有好的想法,当然要尝试着做出来.对滚动条的扩展,现在有很多类是的例子. VS2015的代码编辑是非常强大的,其中有一个功能可以 ...

  6. SQL30081N 检测到通信错误。正在使用的通信协议:"TCP/IP"

    环境描述: 今天在虚拟机上安装了Linux系统,并且装了DB2,但是在连接的时候遇到了个问题,百思不得其解.下面是具体问题跟解决办法 问题描述: 解决办法: 1.先ping服务器是否可以ping通. ...

  7. swift 基于SDK8.0 获取当前时间

    var date = NSDate.date() var timeFormatter = NSDateFormatter() timeFormatter.dateFormat = "MM-d ...

  8. 一、IRIG-B 概念

    参考:http://baike.baidu.com/view/3601618.htm http://wenku.baidu.com/view/7956cd29bd64783e09122bf1.html ...

  9. Asp.Net Web API开发微信后台

    如果说用Asp.Net开发微信后台是非主流,那么Asp.Net Web API的微信后台绝对是不走寻常路. 需要说明的是,本人认为Asp.Net Web API在开发很多不同的请求方法的Restful ...

  10. Linux C 文件与目录4 将缓冲区数据写入磁盘

    将缓冲区数据写入磁盘 所谓缓冲区,是Linux系统对文件的一种处理方式.在对文件进行写操作时,并没有立即把数据写入到磁盘,而是把数据写入到缓冲区.如果需要把数据立即写入到磁盘,可以使用sync函数.用 ...