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
/ \
\_/

题目大意是给定一个无向图,可能有环,实现一个方法,deep clone这个图。

我的做法是采用BFS,从一个点开始,遍历它的邻居,然后加入队列,当队列非空,循环遍历,因为label是唯一的,采用map保存新生成的node,用label作为key。另外使用visited数组保存是否加入过队列,以免重复遍历。

Talk is cheap>>

  public UndirectedGraphNode cloneGraph(UndirectedGraphNode root) {
HashSet<Integer> visited = new HashSet<>();
if (root==null)
return null;
List<UndirectedGraphNode> queue = new ArrayList<>();
HashMap<Integer,UndirectedGraphNode> map = new HashMap<>();
queue.add(root);
while (!queue.isEmpty()) {
UndirectedGraphNode node = queue.get(0);
if (map.get(node.label)==null) {
map.put(node.label, new UndirectedGraphNode(node.label));
}
UndirectedGraphNode tmp = map.get(node.label);
queue.remove(0); for (int i = 0; i < node.neighbors.size(); i++) {
int key = node.neighbors.get(i).label;
if (map.get(key)==null){
map.put(key,new UndirectedGraphNode(key));
}
tmp.neighbors.add(map.get(key));
visited.add(node.label);
if (!visited.contains(key)){
queue.add(node.neighbors.get(i));
visited.add(key);
}
}
}
return map.get(root.label);
}

Clone Graph——LeetCode的更多相关文章

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

  2. Clone Graph [LeetCode]

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

  3. [Leetcode Week3]Clone Graph

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

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

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

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

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

  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. LeetCode: Clone Graph 解题报告

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

  8. 21. Clone Graph

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

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

随机推荐

  1. codevs 1994 排队 排列组合+高精度

    /* 数学题0.0 最后答案:A(n,n)*A(n+1,2)*A(n+3,m)+A(n,n)*C(m,1)*A(2,2)*C(n+1,1)*A(n+2,m-1); 简单解释一下 +之前的很显然 先排男 ...

  2. 一些css小用法总结(持续更新~)

    1.用:before和:after实现小尖角效果 <div class="div"></div> .div{ background: #fff; borde ...

  3. (转)dedecms插件开发简明教程

    这篇文章主要为大家介绍了dedecms插件开发的方法,以实例形式对插件开发的步骤进行了详细的介绍,非常具有实用价值,需要的朋友可以参考下  原文:http://www.jb51.net/cms/230 ...

  4. SVN global ignore pattern for c#

    *.resharperoptions Web_Data log */[Bb]in [Bb]in */obj obj  */TestResults TestResults *.svclog Debug ...

  5. Python算术运算符

    Python 运算符 什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4和5被称为操作数,"+"号为运算符. Python语言支持 ...

  6. iOS8中添加的extensions总结(一)——今日扩展

    通知栏中的今日扩展 分享扩展 Action扩展 图片编辑扩展 文件管理扩展 第三方键盘扩展 注:此教程来源于http://www.raywenderlich.com的<iOS8 by Tutor ...

  7. android 05

    控件:RadioButton CheckedBox RatingBar ProgressBar 下拉列表:ListView Spinner <!-- 单选按钮必须放在单选按钮组当中才能生效 ,并 ...

  8. 用 javascript 判断 IE 版本号

    原文地址: http://julying.com/blog/determine-the-version-number-of-ie-with-javascript/ var _IE = (functio ...

  9. linux makefle学习

    学习材料取之这个网址:http://blog.chinaunix.net/uid-27717694-id-3696246.html 学习环境:ubuntu10.04-64bit-desktop版,gc ...

  10. php 文件操作之抓取网站图片

    $str= file_get_contents("http://v.qq.com/");preg_match_all("/\<img\s+src=.*\s*\> ...