Clone Graph

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

OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
/ \
/ \
0 --- 2
/ \
\_/ 思想: 简单纯粹的深度优先搜索。
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
typedef UndirectedGraphNode NODE;
typedef pair<NODE*, NODE*> PAIR;
NODE* dfs(NODE *node, unordered_map<NODE*, NODE*> &_map) {
if(_map.count(node)) return _map[node];
NODE *p = new NODE(node->label);
_map.insert(PAIR(node, p));
for(size_t i = 0; i < node->neighbors.size(); ++i) {
p->neighbors.push_back(dfs(node->neighbors[i], _map));
}
return p;
} class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
unordered_map<NODE*, NODE*> _map;
_map[NULL] = NULL;
return dfs(node, _map);
}
};

21. Clone Graph的更多相关文章

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

  2. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

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

  4. LeetCode: Clone Graph 解题报告

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

  5. [Leetcode Week3]Clone Graph

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

  6. 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之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  8. [LeetCode] Clone Graph 无向图的复制

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

  9. Clone Graph leetcode java(DFS and BFS 基础)

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

随机推荐

  1. 二模 (15)day1

    第一题: 题目大意: 有两个长度为N的序列A和B,在A和B中各任取一个数相加可以得到N2个和,求这N2个和中最小的N个. 解题过程: 1.这题是刘汝佳<<训练指南>>上的一道经 ...

  2. Linux Shell 脚本入门

    linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...

  3. java基础之 内部类

    Java中的内部类共分为四种: 静态内部类static inner class (also called nested class) 成员内部类member inner class 局部内部类loca ...

  4. linux命令:whereis

    1.命令介绍: whereis用来查找二进制文件,源代码文件和帮助说明文件,whereis是在系统的数据库文件中查找,所以速度非常快,但是系统的文件数据库不是实时更新的,默认一个星期更新一次. 2.命 ...

  5. Unit Tests

    The Three Laws of TDD First Law : you may not write production code until you have written a failing ...

  6. 第三个Sprint冲刺第五天

    讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:继续昨天的工作

  7. 任务太多,时间太少,GT凶猛,不留情面啊。。。

    最近由于提高了发现资料的效率及方法,于是得到了很多好的资料,也打印了好多资料!可是,我突然发现自己好像要做的事太多了,一时间没有了头绪.今天花点时间写个博客,整理一下最近杂乱的状态,看看到底该如何调配 ...

  8. mysql常用&实用语句

    Mysql是最流行的关系型数据库管理系统,也是目前最常用的数据库之一,掌握其常用的操作语句是必不可少的. 下面是自己总结的mysqp常用&实用的sql语句: 1.mysql -u root - ...

  9. 蓝牙协议栈中的 OSAL

    蓝牙协议栈里的操作系统叫做 OSAL(操作系统抽象层).它并非一个真正意义上的操作系统,它只是实现了操作系统的一些功能,如任务切换.内存管理. OSAL 产生的根源:基于蓝牙协议栈开发的产品,实际上是 ...

  10. CCombox使用

    1.得到选中的combox的文本信息 int nIndex = m_SType.GetCurSel();//样本类型 m_SType.GetLBText(nIndex, m_data->STyp ...