[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 ...
随机推荐
- Java如何创建参数个数不限的函数
可变的参数类型,也称为不定参数类型.英文缩写是varargus,还原一下就是variable argument type.通过它的名字可以很直接地看出来,这个方法在接收参数的时候,个数是不定的. pu ...
- [CI]CodeIgniter特性 & 结构
------------------------------------------------------------------------------------------------- 市场 ...
- [PHP]将回调函数作用到给定数组的单元上
---------------------------------------------------------------------------------------------------- ...
- python的垃圾回收机制【转】
http://python.jobbole.com/82061/ http://www.jianshu.com/p/1e375fb40506 https://www.cnblogs.com/vamei ...
- C++ 原来 const 中所使用的函数 必须 全都具有 const 才行
今天在写程序的时候,出现了一个错误 "对象包含与成员函数不兼容的类型限定符",从网上查了一下,原来原因是这样子的 void showPair();改成 void showPair ...
- 梯度迭代树(GBDT)算法原理及Spark MLlib调用实例(Scala/Java/python)
梯度迭代树(GBDT)算法原理及Spark MLlib调用实例(Scala/Java/python) http://blog.csdn.net/liulingyuan6/article/details ...
- ajax-》post
1:最近写完前端,又写后端,jQuery的ajax已经用烂了,事实证明的确好用,再分享一下. data是后台echo的值,可以是数字,也可以是数组,用json_encode()包裹数组形式,前端接收要 ...
- Codeforces Round #499 (Div. 2) C Fly题解
题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...
- java.lang.IllegalArgumentException: Missing either @POST URL or @Url parameter.
以前联调的接口,都是类似这样子的http://ip:8080/WLInterface/register 在baseUrl(http://ip:8080/WLInterface/register ) ...
- 测试工具之appcrawler的使用
appcrawler 标签(空格分隔): appcrawler appcrawler 简介 一个基于自动遍历的app爬虫工具. 支持android和iOS, 支持真机和模拟器. 最大的特点是灵活性. ...