[leetcode]133. Clone Graph 克隆图
题目
给定一个无向图的节点,克隆能克隆的一切
思路
1--2
|
3--5
以上图为例,
node neighbor
1 2, 3
2 1
3 1, 5
5 3
首先从1开始, 将(node,newNode)put到HashMap中
node newNode
1 1
然后遍历该node的所有neighbor
node neighbor
1 2, 3
此时遍历到2
将(node,newNode)put到HashMap中
node newNode
1 1 -- 2 // newNode.neighbors.add(clone)
2 2
代码
public class Solution { Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) {
return null;
} //DFS
if (map.containsKey(node)) {
return map.get(node);
} UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
map.put(node, newNode);
for (UndirectedGraphNode nei : node.neighbors) {
UndirectedGraphNode clone = cloneGraph(nei);
newNode.neighbors.add(clone);
}
return newNode;
}
}
[leetcode]133. Clone Graph 克隆图的更多相关文章
- [LeetCode] 133. Clone Graph 克隆无向图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 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 ...
- 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 ...
- Leetcode#133 Clone Graph
原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...
- 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 ...
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- 【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 ...
- 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 ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
随机推荐
- Echarts(一)
echarts3.61.<!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="barMain" style="heigh ...
- SQL SERVER 2000安装遇到的问题小汇总(转载)
[1]安装程序配置服务器失败需要修改下注册表1 打开注册表 在"开始"--"运行"键入 "regedit" 2 删除注册表如下键值: HK ...
- Cobbler自动化工具实践
1.Cobbler Install 安装前准备 /*注:Cobbler需安装在CentOS7机器上面,建议安装Cobbler机器的CentOS7 everything版本*/ 关闭SELinux c ...
- Visual Studio配置C/C++-PostgreSQL(9.6.3)开发环境(ZT)
https://www.2cto.com/database/201707/658910.html 开发环境 Visual Studio 2017[15.2(26430.16)] PostgreSQL ...
- 25. IO流.md
目录 IO分类: 1.FIle类 1.1目录分隔符 1.2常用方法 2.FileInputStream类 2.1读取文件 3.FileOutputStream类 拷贝文件 4.缓冲流 4.1 Buff ...
- 函数传参传的是啥的思考【java Python】
今天看<java 核心 卷1>的时候,作者提到了函数传参的问题,他提到,java传参,传的是值,而不是引用,然后,函数将要传的实参的值(如果实参是基本数据类型,那么就是值.如果实参是对象, ...
- oracle创建数据库步骤
1.oracle安装成功后, cmd sqlplus,然后system/orcl as sysdba 2.更改scott的密码,scott的默认密码是tiger SQL> alter user ...
- vue--公告轮播
html: <div class="news" v-if="news.length > 0" > <ul class="m ...
- 从后台获取的数据渲染到页面中的dom操作
很多情况下页面dom都是从后台拼接字符串添加生成的新的dom元素,在编辑器中不能看到,只能通过检查看到页面的dom结构,但是这时候会发生一个问题,就是如果使用jQuery无法进行dom操作,事件和方法 ...
- centos7里面docker不能下载本地源
报这种错的 编辑这个文件 加上这一段内容 rstart重启,搞定.