题目:

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

OJ's undirected graph serialization:

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

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
/ \
/ \
0 --- 2
/ \
\_/

链接: http://leetcode.com/problems/clone-graph/

题解:

拷贝图。图的遍历,主要就是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的更多相关文章

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

  2. 【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 ...

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

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

  5. 133. Clone Graph(图的复制)

    Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains ...

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

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

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

  9. [LeetCode] 133. Clone Graph 克隆无向图

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

随机推荐

  1. java中的拷贝文件FileChannel

    以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码: 下边是传统的byte数组拷贝方法 </pre><pre name=&q ...

  2. VMware下LINUX的虚拟机增加磁盘空间

    先关闭虚拟机电源,做如下设置:“ 虚拟机”--“虚拟机设置”--“磁盘”--“扩展” 可以随意添加你需要增到到的磁盘大小(如15Gb,表示磁盘总量,包含原来的磁盘容量); 再重启电源进入系统做如下步骤 ...

  3. Swift 2.0基本语法

    内容包括:01变量&常量 02分支 03循环 04字符串 05数组 06字典 07函数 01变量&常量 //: Playground - noun: a place where peo ...

  4. asp.net 音乐播放器

    <HTML> <HEAD><TITLE>Playback Example</TITLE> </HEAD> <BODY> < ...

  5. HDU分类

    原地址:http://www.byywee.com/page/M0/S607/607452.html 总结了一下ACM STEPS的各章内容,趁便附上我的Steps题号(每人的不一样). 别的,此文首 ...

  6. OpenJudge 2766 最大子矩阵

    1.链接: http://bailian.openjudge.cn/practice/2766 2.题目: 总Time Limit: 1000ms Memory Limit: 65536kB Desc ...

  7. C#拓展练习之模拟键盘录入

    摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 使用C#调用Windows API使程序模拟键盘输入,也可模拟按下快捷键.本例中,单击“模拟输入”按钮,可录入字符“ ...

  8. 超过130个你需要了解的vim命令

    基础 :e filename Open filename for edition :w Save file :q Exit Vim :q! Quit without saving :x Write f ...

  9. Web前端新人笔记之CSS值和单位

    数字 颜色——命名颜色 在Css2.1中规范定义了17个颜色名.包括html4.0中定义的16个颜色及外加一个橙色: <h1 style="color=aqua">aq ...

  10. demo_01 css3中的radius

    css属性:border-radius :border:边框:radius:弧度:所以这个属性的意思很明了. 下面实现一个小demo: <!doctype html> <html&g ...