133克隆图 · Clone Graph
[抄题]:
克隆一张无向图,图中的每个节点包含一个 label
和一个列表 neighbors
。
[思维问题]:
[一句话思路]:
先BFS克隆点(一个点+扩展所有邻居),再克隆邻居(一个点+扩展所有邻居)。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- bfs的结果应该存在一个节点构成的数组中ArrayList<UndirectedGraphNode>,作为for(UndirectedGraphNode n: nodes)循环的已有上限,表示对点的统计
- 最后返回的是mapping.get(headnode);表示新克隆出来的图,不用新定义一个result,因为返回的不是数组
[二刷]:
- 先取node, 再取出每个node的多个neighbor
- copy都只是循环,不是函数,不是没调用呢嘛
- node第一次既要进入queue,也要进入检查的set
- bfs中返回一个新new出来的ArrayList<UndirectedGraphNode>
- corner case返回的是null,想想就知道
[三刷]:
- bfs中,对每一个节点的每一个neighbor都要操作。因此必须循环套循环
- 二级循环中是对临时变量进行操作的,即 for (UndirectedGraphNode neighbor : head.neighbors)中的neighbor
[四刷]:
[五刷]:
[总结]:
- DFS要有栈,可能会stackoverflow,所以尽量用BFS。
- 为保证代码风格良好,特殊情况缩进放短的内容,一般情况不缩进放长的内容。 eg 特殊情况+{continue;} 而一般情况+{长语句;}
[复杂度]:Time complexity: O(边数+点数) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 图和树的区别:从A到B有多条路径
- Queue的实现(右边)有:linkedlist arraylist pq, 实现之后才有offer等方法
- for (UndirectedGraphNode n : manynodes) ,前者是临时变量,后者是数组名,表示遍历。右边是范围上限
[其他解法]:
[Follow Up]:
- 无权图的最短路:哈希表distance(起点,到起点的距离)
- 克隆linkedlist, tree
[LC给出的题目变变变]:
138. Copy List with Random Pointer:也是先复制节点,再复制指针
public class Solution {
/*
* @param node: A undirected graph node
* @return: A undirected graph node
*/
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
//HashMap
HashMap<UndirectedGraphNode,UndirectedGraphNode> mapping = new HashMap<>();
//corner case
if (node == null) {
return null;
}
//bfs
ArrayList<UndirectedGraphNode> manynodes = bfs(node);
//copy node
for (UndirectedGraphNode n : manynodes) {
mapping.put(n,new UndirectedGraphNode(n.label));
}
//copy neighbor
for (UndirectedGraphNode n : manynodes) {
UndirectedGraphNode newNode = mapping.get(n);
for (UndirectedGraphNode neighbor : n.neighbors) {
UndirectedGraphNode newNeighbor = mapping.get(neighbor);
newNode.neighbors.add(newNeighbor);
}
}
//return
return mapping.get(node);
}
//bfs
private ArrayList<UndirectedGraphNode> bfs(UndirectedGraphNode node) {
Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
HashSet<UndirectedGraphNode> set = new HashSet<>(); queue.offer(node);
set.add(node);
while (!queue.isEmpty()) {
UndirectedGraphNode head = queue.poll();
for (UndirectedGraphNode neighbor : head.neighbors) {
if (!set.contains(neighbor)) {
queue.offer(neighbor);
set.add(neighbor);
}
}
}
return new ArrayList<UndirectedGraphNode>(set);
}
}
133克隆图 · Clone Graph的更多相关文章
- LeetCode 133:克隆图 Clone Graph
题目: 给定无向连通图中一个节点的引用,返回该图的深拷贝(克隆).图中的每个节点都包含它的值 val(Int) 和其邻居的列表(list[Node]). Given a reference of a ...
- [Java]LeetCode133. 克隆图 | Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- Java实现 LeetCode 133 克隆图
133. 克隆图 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node { ...
- 【LeetCode】133. 克隆图
133. 克隆图 知识点:图:递归;BFS 题目描述 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[No ...
- Leetcode 133.克隆图
克隆图 克隆一张无向图,图中的每个节点包含一个 label (标签)和一个 neighbors (邻接点)列表 . OJ的无向图序列化: 节点被唯一标记. 我们用 # 作为每个节点的分隔符,用 , 作 ...
- 【leetcode 133. 克隆图】解题报告
方法一:dfs(递归) map<Node*,Node*> dict; Node* clone(Node* node) { if (!node) return node; if (dict. ...
- 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 ...
- 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 ...
随机推荐
- 1036 Boys vs Girls (25 分)
1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of ...
- centos 安装卸载软件命令 & yum安装LAMP环境
安装一个软件时 yum -y install httpd 安装多个相类似的软件时 yum -y install httpd* 安装多个非类似软件时 yum -y install httpd php p ...
- python redis启用线程池管理
pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT,max_connections=3,password=REDIS_PASSWO ...
- 基于WMI获取本机真实网卡物理地址和IP地址
using System; using System.Collections.Generic; using System.Management; using System.Runtime.Intero ...
- python中的logger模块详细讲解
logger 提供了应用程序可以直接使用的接口handler将(logger创建的)日志记录发送到合适的目的输出filter提供了细度设备来决定输出哪条日志记录formatter决定日志记录的最终输出 ...
- 编译器错误消息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v4.0.50727/Temporary ASP.NET Files/root .... 拒绝访问。
此问题困扰良久,得终极解决方案 环境:windows 2008 server r2 + iis7 + .net framework4.5 解决:1. 错误信息中包含的目录“c:/Windows/Mic ...
- django 更新 安装及原理
Django框架简介 MVC框架和MTV框架(了解即可) MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图 ...
- ios tableview header 透明
当将tableview的style属性设为grouped时,header或footer会变成透明,如果设为plain,header或footer会保持默认颜色
- python的线上环境配置
1.安装python 2.7 http://www.cnblogs.com/strikebone/p/3970512.html 2.安装相关前置工具 pip, Django http://www ...
- 编写ios和android共用的c/c++库时 使用iconv的问题(转)
因为在项目中需要同时维护ios和Android,不同的代码不利于开发的便捷和以后的维护,所以在最近的一个项目中,两种手机应用的通信部分打算使用c/c++库来统一编写,ios调用.a静态库,androi ...