leetcode 133. Clone Graph ----- java
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 node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
复制图。有DFS和BFS两种方法,我选用了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; Map map = new HashMap<Integer,UndirectedGraphNode>();
Queue queue = new LinkedList<UndirectedGraphNode>(); queue.add(node);
UndirectedGraphNode nnn = new UndirectedGraphNode(node.label);
map.put(nnn.label,nnn); while( !queue.isEmpty() ){
UndirectedGraphNode nn = (UndirectedGraphNode) queue.poll(); List ll1 = nn.neighbors;
UndirectedGraphNode nn2 = (UndirectedGraphNode) map.get(nn.label);
List ll2 = nn2.neighbors; for( int i = 0;i<ll1.size();i++){ UndirectedGraphNode node2 = (UndirectedGraphNode) ll1.get(i);
if( map.containsKey(node2.label) ){
ll2.add(map.get(node2.label));
}else{
UndirectedGraphNode node3 = new UndirectedGraphNode(node2.label);
map.put(node2.label,node3);
ll2.add(node3);
queue.add(node2);
}
} } return nnn;}
}
leetcode 133. Clone Graph ----- java的更多相关文章
- [LeetCode] 133. Clone Graph 克隆无向图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 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 ...
- [leetcode]133. Clone Graph 克隆图
题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node neighbor 1 2, 3 2 1 3 1 ...
- Leetcode#133 Clone Graph
原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...
- 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 ...
- 【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 Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- 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】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
随机推荐
- Linux-如何添加路由表
linux下静态路由修改命令方法一:添加路由route add -net 192.168.0.0/24 gw 192.168.0.1route add -host 192.168.1.1 dev 19 ...
- Highcharts导出gb2312乱码问题
Highcharts是utf-8编码的,其本地的.net导出环境也是utf-8格式的,导致网页如果采用gb2312编码,显示正常,导出就乱码了.这种现象也同样经常出现在ajax的使用过程中. ajax ...
- redis2.8--c/s架构流程
- awt可视化界面上传数据到mysql,jsp通过jdbc方式查询数据库,并将结果打印在网页上
今天尝试写一个小demo实现下之前看过的代码,目的了解不同文件的数据访问,掌握如何获取前台数据,如何将数据库的数据在前端页面展示. awt可视化界面可已实现提交数据到数据库,也可查询数据在控制台打印. ...
- my class 2.0
www.dropbox.com www.google.com/voice www.prezi.com www.evernote.com
- JS中javascript:void(0)真正含义
对于下面的代码,其中void(0)的含义是什么? <a href="javascript:Test();void(0);">hello</a> 其实,Jav ...
- Python File I/O
File is a named location on disk to store related information. It is used to permanently store data ...
- Volatile vs VolatileRead/Write?
You should never use Thread.VolatileRead/Write(). It was a design mistake in .NET 1.1, it uses a ful ...
- C/C++学习之基础-001
1.C++虚函数的工作原理 虚函数(virtual function)需要虚函数表(virtual table)才能实现.如果一个类有函数声明成虚拟的,就会生成一个虚函数表,存放这个类的虚函数地址.若 ...
- public protected default private
简单来说,如果让一个变量或者方法,只想让自己类中的访问,那么就将它们设置成private 如果你想让一个变量或者方法,本包中的类可以访问,而且子类也可访问,但是包外的缺不想让他访问.就设置成prote ...