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 ...
随机推荐
- C#当中的泛型和java中的对比
1.C#中的泛型 先写一个Demo: namespace generic { public class Program { static ...
- 反汇编windows htonl()函数
因为自己在系统内核写网络程序有时候需要调用htons htonl 这样的函数进行转换,但由于内核只能调用c运行库,别的API不能调用.自己也接触过一点汇编,从来没有去学过.看过老码识途这本书前几章,如 ...
- Centos7最小化安装后(minimal)安装图形界面
centos7下载地址:http://mirrors.cqu.edu.cn/CentOS/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso 下载后用vmwa ...
- CSS3滤镜!!!
<!DOCTYPE html> <html> <head> <style> img { width: 33%; height: auto; float: ...
- checkbox prop()函数
1.设置checkbox选中状态 ①选中: .prop('checked',true); ②不选中:.prop('checked',false); 2.获取checkbox选中状态 .prop('ch ...
- 九度OJ 1349 数字在排序数组中出现的次数 -- 二分查找
题目地址:http://ac.jobdu.com/problem.php?pid=1349 题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n,表示数 ...
- Aspose.word总结
1.创建word模版,使用MergeFeild绑定数据 新建一个Word文档,命名为Template.doc 注意:这里并不是输入"<”和“>”就可以了,而是必须 ...
- DOM五大对象
1.Window 对象:Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个 ...
- 用twisted 做一个日志收集系统
混沌初开 起初我是不会上logging模块的,直接导致了即时有了日志,我也存到了数据库中,而且量也不大,是否能遇到异常只能靠运气了 开天辟地 不得不说,没有任何输出的线上环境真的挺难调试的,当然,聪明 ...
- jquery 验证插件 validate
1)required:true 必输字段(2)remote:"check.php" 使用ajax方法调用check.php验证输入值(3)email:true 必须输入正确格式的电 ...