133. Clone Graph (Graph, Map; DFS)
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 #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
思路:对neughbors的每个节点,如果还没创建,DFS
所以需要一个map标示节点是否已创建
每次递归内容为创建该节点,并递归创建邻节点。
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
}; class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node) return NULL;
UndirectedGraphNode *current;
map<UndirectedGraphNode*,UndirectedGraphNode*> flag; //前一个元素是节点在原Graph中的地址,后一个元素是节点在新拷贝的图中的位置
UndirectedGraphNode *root = cloneNode(node,flag);
return root;
} UndirectedGraphNode * cloneNode(UndirectedGraphNode *source, map<UndirectedGraphNode*,UndirectedGraphNode*> &flag)
{
if(flag.find(source)!= flag.end()) return flag[source]; //如果map中没有该节点,那么创建该节点
UndirectedGraphNode *target = new UndirectedGraphNode(source->label);
flag[source] = target; for(vector<UndirectedGraphNode *>::iterator it = source->neighbors.begin(); it < source->neighbors.end(); it++ )
{
UndirectedGraphNode *newRoot = cloneNode(*it, flag); //深度优先,先递归处理它的某一个邻居,再处理其他邻居
target->neighbors.push_back(newRoot);
}
return target; //返回已经处理好的节点
}
};
133. Clone Graph (Graph, Map; DFS)的更多相关文章
- Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 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 ...
- 1131 Subway Map DFS解法 BFS回溯!
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- LYK loves graph(graph)
题目: LYK loves graph(graph) Time Limit:2000ms Memory Limit:128MB LYK喜欢花花绿绿的图片,有一天它得到了一张彩色图片,这张图片可以看 ...
- 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 ...
- 133. Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- maven+jmeter+jenkins集成
马上要国庆了,最近比较忙,但是感觉忙的效率很一般,之前写过ant的集成,这两天研究了下maven,其中核心的插件便是jmeter-maven-plugin,要想了解更多的朋友,可以自行去官网wiki学 ...
- Ubuntu下MySQL数据库文件 物理迁移后 出现的问题
参考资料: https://www.cnblogs.com/advocate/archive/2013/11/19/3431606.html 本文要解决的一个问题是数据库文件进行物理迁移时遇到的问题. ...
- 494 - Kindergarten Counting Game
Kindergarten Counting Game Everybody sit down in a circle. Ok. Listen to me carefully. ``Woooooo, ...
- Vim: 有关空格和tab的设置,以及如何全文将空格转成tab
这两行可以放在~/.vimrc: :set tabstop=8:set noexpandtab 这个可以每次手动运行,从而将所有的空格转成tab:%retab!
- Linux中的中断处理
1. Linux中中断除了中断分层之外,还有一种就是中断线程化 存在意义:在Linux中,中断具有最高的优先级.不论在任何时刻,只要产生中断事件,内核将立即执行相应的中断处理程序,等到所有挂起的中断和 ...
- nomad 集群搭建
比较简单的集群搭建 一个server 三个client (单机) 参考代码 https://github.com/rongfengliang/nomad-cluster-demo server 配置 ...
- SQL2008如何清空压缩数据库日志
SQL2008如何清空压缩数据库日志 编写人:左丘文 2015-4-10 近期在给一系统初始化资料时,不断的导入导出,因此一不小心,就将数据的SQL(sql2008R2)的是日志档弄得比数据库还大,给 ...
- Tomcat服务器端口的配置
一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...
- PSI分析
"SI是对多个TS流的描述,它包含了PSI" PSI只提供了单个TS流的信息,使接收机能够对单个TS流中的不同节目进行解码:但是,它不能提供多个TS流的相关业务,也不能提供节目的类 ...
- TCP 3-Way Handshake (SYN,SYN-ACK,ACK)
http://www.inetdaemon.com/tutorials/internet/tcp/3-way_handshake.shtml