[leetcode]Q133Clone Graph
克隆图记住:一个map一个queue,照葫芦画瓢BFS
找到一个节点就画一个对应的新的,用map对应,然后添加邻居,记录邻居到queue
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node==null) return node;
UndirectedGraphNode res = new UndirectedGraphNode(node.label);
Queue<UndirectedGraphNode> queue = new LinkedList<>();
Map<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<>();
queue.offer(node);
map.put(node,res);
while (!queue.isEmpty()){
UndirectedGraphNode cur = queue.poll();
List<UndirectedGraphNode> neighbors = cur.neighbors;
for (UndirectedGraphNode neighbor :
neighbors) {
if (!map.containsKey(neighbor)){
UndirectedGraphNode newNode = new UndirectedGraphNode(neighbor.label);
queue.offer(neighbor);
//在旧图中每遍历到一个节点,就在map中给新图申请一个对应的内存
map.put(neighbor,newNode);
}
//根据旧图中的位置为新图添加邻居
map.get(cur).neighbors.add(map.get(neighbor));
}
}
return res;
}
[leetcode]Q133Clone Graph的更多相关文章
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- LeetCode OJ-- Clone Graph **@
https://oj.leetcode.com/problems/clone-graph/ 图的拷贝,就是给一个图,再弄出一个一模一样的来. /** * Definition for undirect ...
- [LeetCode] Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- [leetcode]Clone Graph @ Python
原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...
- [LeetCode] 261. Graph Valid Tree 图是否是树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode:Clone Graph
题目如下:实现克隆图的算法 题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...
- [LeetCode#261] Graph Valid Tree
Problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair o ...
- LeetCode - Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
随机推荐
- [从源码学设计]蚂蚁金服SOFARegistry之消息总线
[从源码学设计]蚂蚁金服SOFARegistry之消息总线 目录 [从源码学设计]蚂蚁金服SOFARegistry之消息总线 0x00 摘要 0x01 相关概念 1.1 事件驱动模型 1.1.1 概念 ...
- MinIO
MinIO 是一个非常轻量的基于 Apache License v2.0 开源协议的对象存储服务.它兼容亚马逊 S3 云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片.视频.日志文件.备份 ...
- 第8.11节 Python类中记录实例变量属性的特殊变量__dict__
一. 语法释义 调用方法:实例. __dict__属性 __dict__属性返回的是实例对象中当前已经定义的所有自定义实例变量的名和值,用字典存储,每个元素为一个"实例变量名:值" ...
- 为什么Python中sort方法和sorted函数调用废弃使用cmp参数
Python中sort方法和sorted函数老猿在前面一些章节介绍过,具体语法及含义在此不再展开说明,但老猿在前面学习相关内容时,只使用了简单的案例,对这两个方法的key参数没有深入研究,总以为就是以 ...
- PyQt(Python+Qt)学习随笔:QTableWidget的findItems和selectedItems搜索项和访问选中项方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.搜索项 在表格部件中,可以根据文本以及匹配模式来搜索满足条件的项,调用语法: list[QTab ...
- Hive基本原理及配置Mysql作为Hive的默认数据库
Hive是什么? Hive是基于Hadoop之上的数据仓库: Hive是一种可以存储.查询.分析存储在hadoop中的大规模数据 Hive定义了简单的类SQL查询语言,成为HQL,它允许熟悉SQL的用 ...
- SQL直接生成实体属性,简单粗暴型
在java开发中,不可避免的要碰到根据表生成对应的实体,这个过程是比较机器且繁琐的,我也用过一些逆向工程的工具,比如IDEA自带的生成实体,还有网上开源的工具,用起来也是可以的. 我现在开发用的持久层 ...
- 一、Nginx笔记--linux下载安装部署Nginx
Nginx 到底是什么? Nginx 是⼀个⾼性能的HTTP和反向代理web服务器,核⼼特点是占有内存少,并发能⼒强 Nginx ⼜能做什么事情(应⽤场景) Http服务器(Web服务器) 性能⾮常 ...
- Jwt令牌创建
添加依赖 <dependencies> <!-- jwt --> <dependency> <groupId>io.jsonwebtoken</g ...
- STL——容器(deque) deque 的赋值 assign() operator=() swap()
deque 的赋值分下边4种方法: deque.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身.注意该区间是左闭右开的区间. 1 #include <io ...