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

First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
    Second node is labeled as 1. Connect node 1 to node 2.
    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
         / \
         \_/
解题思路:

BFS或DFS均可做出,BFS的JAVA实现如下:

    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null)
return null;
Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
queue.add(node);
map.put(node, newHead); while (!queue.isEmpty()) {
UndirectedGraphNode curr = queue.poll();
List<UndirectedGraphNode> currNeighbors = curr.neighbors;
for (UndirectedGraphNode aNeighbor : currNeighbors)
if (!map.containsKey(aNeighbor)) {
UndirectedGraphNode copy = new UndirectedGraphNode(
aNeighbor.label);
map.put(aNeighbor, copy);
map.get(curr).neighbors.add(copy);
queue.add(aNeighbor);
} else
map.get(curr).neighbors.add(map.get(aNeighbor));
}
return newHead;
}

DFS 实现如下:

	Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();

	public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null)
return null;
if (map.containsKey(node))
return map.get(node);
UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
map.put(node, newHead);
for (UndirectedGraphNode aNeighbor : node.neighbors)
newHead.neighbors.add(cloneGraph(aNeighbor));
return newHead;
}

Java for LeetCode 133 Clone Graph的更多相关文章

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

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

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

  3. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  4. Leetcode#133 Clone Graph

    原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...

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

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

  7. 133. Clone Graph

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

  8. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  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. myeclipse执行tomcat报错Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: PermGen space

    将myeclipse所配置的tomcat的jdk进行设置:-Xms512m -Xmx512m -XX:MaxNewSize=512m -XX:MaxPermSize=512m,例如以下图:

  2. 时间迭代和BigDecimal操作

    常规小操作的代码: import java.math.BigDecimal; import java.sql.Timestamp; import java.text.SimpleDateFormat; ...

  3. Mongo-Hadoop

    下载 https://github.com/mongodb/mongo-hadoop/releases 解压到/home/kevin/hadoop/hadoop/share/mongo-hadoop- ...

  4. sakila演示数据库安装

    下载地址:https://dev.mysql.com/doc/index-other.html 安装帮助文档:https://dev.mysql.com/doc/sakila/en/sakila-in ...

  5. AWS上的游戏服务:Lumberyard + Amazon GameLift + Twitch

    开发一款世界级的游戏是一个非常困难,耗时和昂贵的过程.如今的游戏玩家要求越来越苛刻,他们希望既能够通过各种不同的终端设备来进行游戏 ,又要求游戏具有社交的功能. 因为此类游戏的开发期和推广期都非常长. ...

  6. spring中xml配置和autowired混用

    1.类的混用: 配置文件中的配置: <bean id="a" class="com.ab.cc.A" /> 类中的配置 @Autowired A a ...

  7. rtems 4.11 工具链

    4年前,曾经把rtems4.10移植到atmel 9263上,要不是当时移植的git仓库还在的话,真是不相信自己居然还干过这事.所以这次再捡起的时候,要记录一下.还是从编译器开始. 首先打开 http ...

  8. 【Java】使用@Value @Reource或@Autowire依赖 (值) 注入时出现NPE的排查方法

    首先想说明的是,@Value @Resource和@Autowire虽然都是用于依赖注入的Annotation,但是二者是有区别的. 1 Resource不依赖于Spring,后者相反,因此为了减少以 ...

  9. Paxos算法学习

    早在1990年,Leslie Lamport(即 LaTeX 中的"La",微软研究院科学家,获得2013年图灵奖)向ACM Transactions on Computer Sy ...

  10. HDFS源码分析心跳汇报之整体结构

    我们知道,HDFS全称是Hadoop Distribute FileSystem,即Hadoop分布式文件系统.既然它是一个分布式文件系统,那么肯定存在很多物理节点,而这其中,就会有主从节点之分.在H ...