题目

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

思路

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. Runloop原理

    简单的说,runloop是一个事件循环的机制,同时能够保活线程.iOS中每个线程都对应一个runloop,主线程的runloop默认开启,其他线程的runloop默认关闭,线程与runloop是一一对 ...

  2. 更改html代码后网页不更新

    写了一个非常简单的 html 页面,只有简单的跳转功能,但是在 Eclipse 下更改代码后用 chrome 浏览器打开时还是显示原来的网页.开始我以为是网页有错误或者有不规范的地方,因为我编写的是 ...

  3. jquery接触初级----jquery 对象和Dom对象

    1. DOM 对象,每一份DOm对象(Document Object model)都可以表示成一棵树,一个基本的网页如下: <!DOCTYPE html> <html lang=&q ...

  4. Mysql(MyISAM和InnoDB)及Btree和索引优化

    MYSQL 一.引擎 mysql:MySQL是一个关系型数据库管理系统,其中有两种引擎最为常见MyISAM和InnoDB MyISAM(非聚集索引)  MySQL 5.0 之前的默认数据库引擎,最为常 ...

  5. ReultSet有什么作用和使用

    结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数据的更新等. int col ...

  6. 简单实现"回车!=提交"(去除表单的回车即提交)

    -------------------------------------------------------------------------------------------------- 实 ...

  7. 9.mysql-存储过程.md

    目录 创建 创建 -- 创建存储过程 DELIMITER $ -- 声明存储过程的结束符 CREATE PROCEDURE pro_test() --存储过程名称(参数列表) BEGIN -- 开始 ...

  8. Python模块和类.md

    模块的定义 代码的层次结构 对于python的层次结构一般为包->模块 包也就是文件夹,但是文件夹下必须有文件"init.py"那么此文件夹才可以被识别为包."in ...

  9. ajax跨域名

    跨域环境模拟: 修改host文件 三种解决的方案 1:ifram(display:none) 2:jsonp(注意是只是适合的是get请求) 生成一个带有src的script标签, 3:cros(后台 ...

  10. iOS 两个页面之间的跳转

    -------->-------->-------->-------->-------->-------->-------->   以上完成页面one跳到页面 ...