题目

给定一个无向图的节点,克隆能克隆的一切

思路

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 克隆图的更多相关文章

  1. [LeetCode] 133. Clone Graph 克隆无向图

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

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

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

  4. Leetcode#133 Clone Graph

    原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...

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

  6. [LeetCode] Clone Graph 克隆无向图

    Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...

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

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

  9. [Leetcode Week3]Clone Graph

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

随机推荐

  1. CSS内容简单总结

    day50 1. 内容回顾 1. 伪类和伪元素        1. 伪类            1. :link            2. :visited            3. :hover ...

  2. Delphi TMemoryStream写入到字符串和字符串写入到流

    一.TMemoryStream数据写入到字符串里 var lvStream:TMemoryStream; s:AnsiString; p: PAnsiChar; begin lvStream:= TM ...

  3. Spark/Storm/Flink

    https://www.cnblogs.com/yaohaitao/p/5703288.html  Spark Streaming与Storm的应用场景 对于Storm来说:1.建议在那种需要纯实时, ...

  4. linux 2.6.32文件系统的dentry父子关系

    我们知道,linux文件系统,inode和dentry是有对应关系的,dentry是文件名或者目录的一个管理结构,2.6内核中: struct dentry { atomic_t d_count; u ...

  5. Linux crontab使用方法

    crontab命令主要用于设置命令行或者脚本周期性的执行.该命令从标准输入设备读取指令,并将其存放于文件中,以供之后读取和执行.本文主要讲述crontb命令的基本语法和配置方法. 1.crontab命 ...

  6. 新版openvpn for pc使用旧证书问题的处理

    在client.ovpn中增加一句: tls-cipher "DEFAULT:@SECLEVEL=0"

  7. 如何遍历List对象

    for(String str : list) {//其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用. System.out.println(str); } .普通for循环 ...

  8. 8.mysql-基础.md

    目录 数据库管理 查看当前软件中的数据库 工具 创建数据库 删除数据 查看字符集 修改数据库 表管理 进入数据库 查看表 创建表 查看表结构 删除表 修改表 添加字段 删除字段 修改字段名称 修改字段 ...

  9. MVC part4

    SpringMVC 注解 @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写,你也可以自己指定, 如下 方法一: @Controller ...

  10. javascript 中Array.prototype.sort 函数的用法

    来源:http://www.jb51.net/article/5769.htm JavaScript中对变量的操作都是通过引用方式,而对数组也一样. 前两天想要对一个数组进行复制,一直苦于找不到办法( ...