[抄题]:

克隆一张无向图,图中的每个节点包含一个 label 和一个列表 neighbors

[思维问题]:

[一句话思路]:

先BFS克隆点(一个点+扩展所有邻居),再克隆邻居(一个点+扩展所有邻居)。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. bfs的结果应该存在一个节点构成的数组中ArrayList<UndirectedGraphNode>,作为for(UndirectedGraphNode n: nodes)循环的已有上限,表示对点的统计
  2. 最后返回的是mapping.get(headnode);表示新克隆出来的图,不用新定义一个result,因为返回的不是数组

[二刷]:

  1. 先取node, 再取出每个node的多个neighbor
  2. copy都只是循环,不是函数,不是没调用呢嘛
  3. node第一次既要进入queue,也要进入检查的set
  4. bfs中返回一个新new出来的ArrayList<UndirectedGraphNode>
  5. corner case返回的是null,想想就知道

[三刷]:

  1. bfs中,对每一个节点的每一个neighbor都要操作。因此必须循环套循环
  2. 二级循环中是对临时变量进行操作的,即 for (UndirectedGraphNode neighbor : head.neighbors)中的neighbor

[四刷]:

[五刷]:

[总结]:

  1. DFS要有栈,可能会stackoverflow,所以尽量用BFS。
  2. 为保证代码风格良好,特殊情况缩进放短的内容,一般情况不缩进放长的内容。 eg 特殊情况+{continue;} 而一般情况+{长语句;}

[复杂度]:Time complexity: O(边数+点数) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 图和树的区别:从A到B有多条路径
  2. Queue的实现(右边)有:linkedlist arraylist pq, 实现之后才有offer等方法
  3. for (UndirectedGraphNode n : manynodes) ,前者是临时变量,后者是数组名,表示遍历。右边是范围上限

[其他解法]:

[Follow Up]:

  1. 无权图的最短路:哈希表distance(起点,到起点的距离)
  2. 克隆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的更多相关文章

  1. LeetCode 133:克隆图 Clone Graph

    题目: 给定无向连通图中一个节点的引用,返回该图的深拷贝(克隆).图中的每个节点都包含它的值 val(Int) 和其邻居的列表(list[Node]). Given a reference of a ...

  2. [Java]LeetCode133. 克隆图 | Clone Graph

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

  3. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  4. Java实现 LeetCode 133 克隆图

    133. 克隆图 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node { ...

  5. 【LeetCode】133. 克隆图

    133. 克隆图 知识点:图:递归;BFS 题目描述 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[No ...

  6. Leetcode 133.克隆图

    克隆图 克隆一张无向图,图中的每个节点包含一个 label (标签)和一个 neighbors (邻接点)列表 . OJ的无向图序列化: 节点被唯一标记. 我们用 # 作为每个节点的分隔符,用 , 作 ...

  7. 【leetcode 133. 克隆图】解题报告

    方法一:dfs(递归) map<Node*,Node*> dict; Node* clone(Node* node) { if (!node) return node; if (dict. ...

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

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

随机推荐

  1. 远程复制文件scp使用

    1. install sudo apt-get install openssh-client openssh-server 2. login ssh remoteuser@remoteIP 3. co ...

  2. 【BZOJ】1975 [Sdoi2010]魔法猪学院(A*)

    题目 传送门:QWQ 分析 k短路,Astar.估价函数是终点向外跑的最短路. 显然不是正解qwq. 代码 // By noble_ // Astar algorithm // #include &l ...

  3. Linux常用命令的命名来源

    很多人在学习Linux的时候会疑惑:这么多的Linux名,他们都是怎么被定义的?林纳斯是怎么制定如此花样繁多且数量庞大的命令?今天这篇文章可能会帮你解开疑惑. ## 1. 目录缩写 缩写 | 全称 | ...

  4. linux read 系统调用剖析

    https://www.ibm.com/developerworks/cn/linux/l-cn-read/ MT注:原文图1与Understanding the Linux Kernel, 3rd ...

  5. uva-10098

    所有的排列,但是要不重复 #include<stdio.h> #include<iostream> #include<sstream> #include<qu ...

  6. python2.7实现websocket服务器,可以在web实时显示远程服务器日志

    一.开始的话 使用python实现websocket服务器,可以在浏览器上实时显示远程服务器的日志. 之前写了一个发布系统,每次发布版本后,为了了解发布情况(进度.是否有错误)都会登录到服务器上查看日 ...

  7. viewer 照片查看器

    viewer 照片查看器 效果: api: https://github.com/fengyuanchen/viewerjs#methods npm: npm install viewerjs 使用: ...

  8. NRF51822之DFU使用手机升级

    演示的工程是 [application]    nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble_app_hrs\pca10028\s110_w ...

  9. Sender 转换TButtonItem TCategoryButtons

    http://codeverge.com/embarcadero.cppbuilder.using/using-sender-to-determine-which/1068317 http://qc. ...

  10. overflow: auto 图片自适应调整

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...