[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 ...
随机推荐
- cp命令 复制目录
格式:cp -R {源目录地址} {目标目录地址} 假设文件夹download下有两个目录 download/a download/b 现在想将文件夹a复制到文件夹b下,执行 cp -R downlo ...
- 为什么MySQL不推荐使用子查询和join
前言: 1.对于mysql,不推荐使用子查询和join是因为本身join的效率就是硬伤,一旦数据量很大效率就很难保证,强烈推荐分别根据索引单表取数据,然后在程序里面做join,merge数据. 2.子 ...
- 运维中的日志切割操作梳理(Logrotate/python/shell脚本实现)(转)
对于Linux系统安全来说,日志文件是极其重要的工具.不知为何,我发现很多运维同学的服务器上都运行着一些诸如每天切分Nginx日志之类的CRON脚本,大家似乎遗忘了Logrotate,争相发明自己的轮 ...
- 利用nginx搭建RTMP视频点播、直播、HLS服务器(转)
开发环境 Ubuntu 14.04 server nginx-1.8.1 nginx-rtmp-module nginx的服务器的搭建 安装nginx的依赖库 sudo apt-get update ...
- genymotion使用学习
1 安装 直接去其官网(https://www.genymotion.com/#!/download)下载安装包安装即可,安装中会附带安装VirtualBox. 2 注册 必须使用帐号登录后,方可下载 ...
- 解决mysql从windows迁移到centos出现乱码问题
windows上的数据库编码情况 修改前centos上的编码情况 修改centos上mysql的配置文件my.cnf如下(修改前停掉mysql服务,/etc/init.d/mysqld stop) 红 ...
- HttpURLConnection 添加代理
//创建代理服务器 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("www.proxyaddress.com& ...
- 4.HTTP入门.md
目录 什么是http协议 http协议:对浏览器客户端 和 服务器端 之间数据传输的格式规范 查看http协议的工具* 使用火狐的firebug插件(右键->firebug->网络) Ht ...
- sqlalchemy 学习--单表操作
以下所有代码片段都使用了统一的引用,该引用如下: from sqlalchemy import create_engine, ForeignKey from sqlalchemy.ext.declar ...
- ArcGIS案例学习笔记4_1_矢量校正
ArcGIS案例学习笔记4_1_矢量校正 概述 计划时间:第四天上午 教程:Editing编辑教程 pdf 目的:矢量数据的空间校正 案例1:仿射变换 数据:Editing编辑数据/spatialAd ...