133. Clone Graph
题目:
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
Nodes are labeled uniquely.
We use #
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}
.
The graph has a total of three nodes, and therefore contains three parts as separated by #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
题解:
拷贝图。图的遍历,主要就是DFS和BFS, 这道题考察基本功。需要注意的地点是如何建立visited数组,这道题因为lable unique,所以可以建立Map<Integer, UndirectedGraphNode>,假如lable有重复值,则建立Map的时候要使用Map<UndirectedGraphNode, UndirectedGraphNode>。 基本题目要多练习,这样拓展到难题以后才能借鉴思路。二刷的时候要注意recursive和iterative。
DFS:
Time Complexity - O(n), Space Complexity - O(n)
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
HashMap<Integer, UndirectedGraphNode> visited = new HashMap<>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node == null)
return node;
if(visited.containsKey(node.label))
return visited.get(node.label); UndirectedGraphNode clone = new UndirectedGraphNode(node.label);
visited.put(clone.label, clone); for(UndirectedGraphNode neighbor : node.neighbors)
clone.neighbors.add(cloneGraph(neighbor)); return clone;
}
}
BFS:
Time Complexity - O(n), Space Complexity - O(n)
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node == null)
return node; Queue<UndirectedGraphNode> q = new LinkedList<>();
q.offer(node);
HashMap<Integer, UndirectedGraphNode> visited = new HashMap<>();
visited.put(node.label, new UndirectedGraphNode(node.label)); while(!q.isEmpty()) {
UndirectedGraphNode newNode = q.poll(); for(UndirectedGraphNode neighbor : newNode.neighbors) {
if(!visited.containsKey(neighbor.label)) {
q.offer(neighbor);
visited.put(neighbor.label, new UndirectedGraphNode(neighbor.label));
}
visited.get(newNode.label).neighbors.add(visited.get(neighbor.label));
}
} return visited.get(node.label);
}
}
二刷:
Java:
DFS:
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
Map<Integer, UndirectedGraphNode> map = new HashMap<>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) return null;
if (map.containsKey(node.label)) return map.get(node.label);
UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
map.put(newNode.label, newNode); for (UndirectedGraphNode neighbor : node.neighbors) {
newNode.neighbors.add(cloneGraph(neighbor));
}
return newNode;
}
}
BFS:
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) return null;
Queue<UndirectedGraphNode> q = new LinkedList<>();
q.offer(node);
Map<Integer, UndirectedGraphNode> visited = new HashMap<>();
visited.put(node.label, new UndirectedGraphNode(node.label)); while (!q.isEmpty()) {
UndirectedGraphNode oldNode = q.poll();
for (UndirectedGraphNode neighbor : oldNode.neighbors) {
if (!visited.containsKey(neighbor.label)) {
q.offer(neighbor);
visited.put(neighbor.label, new UndirectedGraphNode(neighbor.label));
}
visited.get(oldNode.label).neighbors.add(visited.get(neighbor.label));
}
}
return visited.get(node.label);
}
}
Reference:
http://www.cnblogs.com/springfor/p/3874591.html
http://blog.csdn.net/linhuanmars/article/details/22715747
http://blog.csdn.net/fightforyourdream/article/details/17497883
http://www.programcreek.com/2012/12/leetcode-clone-graph-java/
https://leetcode.com/discuss/26988/depth-first-simple-java-solution
https://leetcode.com/discuss/44330/java-bfs-solution
https://leetcode.com/discuss/14969/simple-java-iterative-bfs-solution-with-hashmap-and-queue
133. Clone Graph的更多相关文章
- 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】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 133. Clone Graph ----- java
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 133. Clone Graph(图的复制)
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains ...
- 133. Clone Graph (Graph, Map; DFS)
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))
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 克隆无向图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
随机推荐
- web框架--来自维基百科
- HW—可怕的阶乘n!__注意大数据函数的使用BigInteger
java.math.BigInteger系列教程(四)BigInteger的诞生原因 为什么java里面要出现BigInteger类型呢?相信很多人有这个疑问,其实原因很简单,它可以表达更大范围的数值 ...
- (七)Hibernate 映射继承
所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:每个具体类对应一个表 Image.java package co ...
- 用法简单的图片和视频播放的框架Demo
最近在恶补自己不足的基础知识,偶然在一个QQ群里看到作为同行业的大神们在开源自己的代码.并且在炫耀说让我们找Bug,于是出于好奇就看了下,点开了一个关于图片和视频播放的Demo.也就是接下来我要说的这 ...
- 第十二篇、HTML常用框架收集
1.Swiper 广告轮播插件 2.Bootstrap 响应式布局 3.jQuery js兼容插件 4.jQuery Mobile 5.phoneGap
- Oracle PL/SQL 事物处理 银行转账
Oracle数据库中的事务处理:添加,修改,删除时需要使用事务处理(显示事务). 1.事务的分类显示事务(添加,修改,删除)和隐式事务(除了添加,修改,删除). 2.事务的执行方式:自动提交(jdbc ...
- Wix: Using Patch Creation Properties - Small Update
Source Reference: wix help document -- WiX Toolset License Using Patch Creation Properties A patch ...
- VS2010 error RC2135: file not found
VS2010 C++ win32 DLL 工程, 添加 rc 文件, 编辑 String Table. 默认情况下英文版本的 rc 文件能够顺序编译通过,为了让工程支持多语言,将字符串修改为其他语言时 ...
- 第48条:如果需要精确的答案,请避免使用float和double
float和double主要为了科学计算和工程计算而设计,执行二进制浮点运算,这是为了在广泛的数值范围上提供较为精确的快速近似计算而精心设计的.然而,它们没有提供完全精确的结果,所以不适合用于需要精确 ...
- 队列(链式存储)C++模板实现
#include <iostream> using namespace std; //队列结点类 template <typename T> class QueueNode{ ...