Clone Graph——LeetCode
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
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 node0to both nodes1and2. - Second node is labeled as
1. Connect node1to node2. - Third node is labeled as
2. Connect node2to node2(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的更多相关文章
- 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. ...
- Clone Graph [LeetCode]
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- 【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 ...
- LeetCode: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- 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 ...
随机推荐
- Reso | liunx下longeneQQ和搜狗拼音
sogoupinyin_2.0.0.0078_amd64.deb: http://pan.baidu.com/s/1eSDLvEU WineQQ7.8-20151109-Longene .deb: ...
- RESTEasy + JBOSS 7 Hello world application---reference
RESTEasy is JBOSS provided implementation of JAX-RS specification for building RESTful Web Services ...
- KVM与VMware的性能比较
结合网页http://www.linuxidc.com/Linux/2011-01/31755.htm等整理 物理环境内存4G ,CPU4个,动态硬盘120G KVM: 硬盘测试瞬间读取速度Timin ...
- [转] prerender-SPA程序的SEO优化策略
随着web2.0的兴起,ajax的时代已经成为了事实,更如今 Knockout,backbone, angular,ember前端MDV(model driver view)框架强势而来,Single ...
- vim 学习相关记录
VIM 相关内容****************** vim 的三个模式: 编辑模式 --> 输入模式 --> 末行模式 编辑模式: 通常键入键盘值被理解成一个操作; 如: dd(删除行) ...
- UIView不能使用UITableView的Static表格的解决方法
在UIView中嵌入一个Container,用Container来包含UITableViewController即可,到storyboard上显示如下:
- Android -- 官方下拉刷新SwipeRefreshLayout
V4的兼容包 API 大概就这4个常用的方法. code 布局 <RelativeLayout xmlns:android="http://schemas.android.com/ap ...
- Windows下Android Studio长时间停留在Building "Project Name" Gradle project info画面的解决方法
问题描述: 创建好一个Android项目后,Android Studio长时间停留在Building [Project Name] Gradle project info画面不动. 原因: 此时And ...
- AS 进行单元测试
以下为本人在AndroidStudio 2.0 上实测后得出的结论,不像网上那一堆堆的误人子弟的文章,都是过时的或者根本就是不对的. 简介 和eclipse需要配置清单文件不同,AndroidStud ...
- 一些 Windows 系统不常见的 鼠标光标常数
一些 Windows 系统不常见的 鼠标光标常数 Private Declare Function SetCursor Lib "user32" (ByVal hCursor A ...