原题地址

方法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 expect初识

    写个命令,让ssh服务器便捷点 #!/usr/bin/expect set type [lindex $argv 0] if {$type == "server"} { set i ...

  2. LtUpload上传组件

    <?php/** * The Upload class * @author Alex Lee <iuyes@qq.com> * @license http://opensource. ...

  3. DevExpress 表中数据导出

    gridView1.ExportToXlsx("SampleStock.xlsx"); if (true) { DevExpress.XtraEditors.XtraMessage ...

  4. Python基础 第二天

    1.http://www.cnblogs.com/beer/p/5672678.html requests和beautifulsoup

  5. Pycharm 使用 (一)

    学习[Python基础教程]到后面的练习阶段就觉得python自带的IDLE有点out的感觉,于是就在网上搜索好用的IDE, 挺多人推荐Pycharm的 不仅跨平台而且还支持django等框架; 初次 ...

  6. 关于activity_main.xml与fragment_main.xml

    第一种解决办法 新版安装SDK文件一开始有两个XML文件,activity_main.xml和fragment_main.xml,不习惯的可以这样处理:1.删除fragment_main.xml整个文 ...

  7. Base

    base 关键字用于从派生类中访问基类的成员: 调用基类上已被其他方法重写的方法. 指定创建派生类实例时应调用的基类构造函数. 基类访问只能在构造函数.实例方法或实例属性访问器中进行. 从静态方法中使 ...

  8. hdu 2275 Kiki & Little Kiki 1

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2275 题意:n个操作 Push 入容器 Pop弹出一个 满足<=该数的最大的数(若没有输出No ...

  9. iOS-系统自带navigationController-最全设置

    // 导航栏背景色 self.navigationController.navigationBar.barTintColor = [UIColor orangeColor]; // 设置push出来的 ...

  10. php判断来源网址地址并且限制非法来源

    $fromHost = array( 'paipai.com', 'localhost', '127.0.0.1' ); $s = 'http://www.paipai.Com/chong/abc.s ...