题目:

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. Service的一些使用

    service服务一般主要是作为后台服务使用的,前台服务一般结合通知一起. service一般主要用作长期后台服务的,而且和Activity结合性不那么紧密, 一般如果需要频繁的更新UI主要是用Act ...

  2. FreeMarker语法2

    FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...

  3. 九度OJ 1512 用两个栈实现队列 【数据结构】

    题目地址:http://ac.jobdu.com/problem.php?pid=1512 题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 输入: 每 ...

  4. 模板:使用new delete 创建二维数组

    int **arr_matrix = new int*[n]; ; i < n; ++i) arr_matrix[i] = new int[n]; //内容 ; i < n; ++i) d ...

  5. Poj 1503 Integer Inquiry

    1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS   Memory Limit: 1000 ...

  6. RabbitMQ远程访问配置

    1 首先创建一个新的账户 并给上Administrator标签 2然后给这个新账户添加虚拟主机访问权限 3在windows 下的 rabbitmq安装文件下的etc文件下的配置文件添加以下 [    ...

  7. CentOS7 IP自动获取

    /etc/sysconfig/network-scripts HWADDR=00:15:5D:00:76:04TYPE=EthernetBOOTPROTO=dhcpDEFROUTE=yesPEERDN ...

  8. Yii框架下使用redis做缓存,读写分离

    Yii框架中内置好几个缓存类,其中有memcache的类,但是没有redis缓存类,由于项目中需要做主从架构,所以扩展了一下: /** * FileName:RedisCluster * 配置说明 * ...

  9. IOS中如何判断APP是否安装后首次运行或升级后首次运行

    对于是否为首次安装的App可以使用如下方法来判断 [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] ...

  10. hdu 5652 India and China Origins 并查集+逆序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...