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 ...
随机推荐
- javascript: 数据类型深入理解
1.基本类型(值类型或者原始类型): Number.Boolean.String.NULL.Undefined以及ES6的Symbol2.引用类型:Object.Array.Function.Date ...
- SharePoint 2013的100个新功能之开发
一:SharePoint应用 SharePoint 2013引入了云应用模型来供用户创建应用.SharePoint应用是独立的功能整合,扩展了SharePoint网站的功能.应用可以包含SharePo ...
- yarn import 使用package-lock.json
yarn 1.7(目前最新的版本)支持npm 的package-lock.json 了 环境准备 安装更新yarn sudo npm install -g yarn 查看版本 yarn version ...
- 浅谈ASP.NET ---- 系列文章
[01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作篇)(下) [04]浅谈ASP. ...
- SonarQube使用教程
SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误,本文将会介绍一下这个工具的安装.配置以及使用. 一.SonarQube的安装使用: 下载地址:http://ww ...
- Java多线程编程核心技术,第四章
1,ReentrantLock 2,object的wait(),wait(x),notify(),notifyAll(),分别等于Condition的await(),await(x,y),signal ...
- 转转转--oracle 去重并按时间排序取第一条
select t.* from (select a.*, row_number() over(partition by 需要分组的字段 order by 更新时间 desc) rw from 表 a) ...
- java对图片进行操作,仅仅是小demo
package com.cy.thumb; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io ...
- 马士兵Spring-hibernate整合
spring整合hibernate: 1.sessionFactory只需要一个就可以了,单例,适合spring管理: 2.HIbernate中的SessionFactory是接口:spring中实现 ...
- Java-Runoob-高级教程-实例-字符串:04. Java 实例 - 字符串替换
ylbtech-Java-Runoob-高级教程-实例-字符串:04. Java 实例 - 字符串替换 1.返回顶部 1. Java 实例 - 字符串替换 Java 实例 如何使用java替换字符串 ...