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
/ \
\_/
 /**
* 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; ArrayList<UndirectedGraphNode> nodes = new ArrayList<>();
HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>(); // clone nodes
nodes.add(node);
map.put(node, new UndirectedGraphNode(node.label)); int start = ;
while (start < nodes.size()) {
UndirectedGraphNode head = nodes.get(start++);
for (UndirectedGraphNode neighbor : head.neighbors) {
if (!map.containsKey(neighbor)) {
map.put(neighbor, new UndirectedGraphNode(neighbor.label));
nodes.add(neighbor);
}
}
} // clone neighbors
for (UndirectedGraphNode originalNode : nodes) {
UndirectedGraphNode copyNode = map.get(originalNode);
for (UndirectedGraphNode neighbor : originalNode.neighbors) {
copyNode.neighbors.add(map.get(neighbor));
}
} return map.get(node);
}
}

Clone Graph的更多相关文章

  1. 21. Clone Graph

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

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

  3. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

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

  5. LeetCode: Clone Graph 解题报告

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

  6. [Leetcode Week3]Clone Graph

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

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

  8. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  9. [LeetCode] Clone Graph 无向图的复制

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

  10. Clone Graph leetcode java(DFS and BFS 基础)

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

随机推荐

  1. ILMerge

    ILMerge http://www.microsoft.com/en-hk/download/details.aspx?id=17630 ILMerge 下载地址:http://www.micros ...

  2. MySQL 视图的基础操作(五)

    1.为什么使用视图:     为了提高复杂SQL语句的复用性和表操作的安全性(例如:工资字段不想展示给所有能查看该查询结果的人),MySQL提供了视图特性.所谓视图,本质上是一种虚拟表,其内容与真实的 ...

  3. OC-self关键字

    self关键字 1. 成员变量和局部变量同名 当成员变量和局部变量同名时,采取就近原则,访问的是局部变量 用self访问成员变量,区分同名的局部变量 2.使用细节 1)     出现的地方:所有的OC ...

  4. [问题] UISearchBar 点击取消后跳动的问题

    问题详情: 首先是TableView 作为 NavigationController 的 RootViewContrller, 然后UISearchBar   添加到TableView 的 headV ...

  5. Cucumber命令行接口

    1. cucumber的命令行选项 首先查看命令行选项.和其它命令行工具一样,cucumber提供了—help选项.下面是cucumber帮助的一个缩减版本: $ cucumber --help -r ...

  6. System.nanoTime与System.currentTimeMillis的区别

    平时产生随机数时我们经常拿时间做种子,比如用 System.currentTimeMillis的结果,但是在执行一些循环中使用了System.currentTimeMillis,那么每次的结 果将会差 ...

  7. WebService 之 WSDL文件 讲解

    原文地址:http://blog.csdn.net/tropica/archive/2008/11/02/3203892.aspx 恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时 ...

  8. oracle 中的dual表简介与用法

    Dual表是每个数据库创建时默认生成的,该表仅有一列一行. 1)分析dual表执行,如下:

  9. #define 中#和##的作用

    #的作用是把后面的参数变成一个字符串. 如,#define f(a) #a f(hello world)相当于"hello world": ##的作用是把两个字符串连接起来. 如, ...

  10. jquery datagrid加载后仅选定第一行

    function onLoadSuccess(data) { var rows = $("#DataGrid").datagrid("getRows"); if ...