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

复制图。有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的更多相关文章

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

  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. [Leetcode Week3]Clone Graph

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

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

  9. 【LeetCode】133. Clone Graph 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

随机推荐

  1. 未能加载文件或程序集“System.WEB.DataVisualization, Version=3.5.0.0, Culture=neutral

    项目打开 提示 如题错误. 最近用VS2010 + .NET Framework3.5SP1开发程序,使用了MsChart,但是部署到服务器的时候提示如下错误: 分析器错误消息: 未能加载文件或程序集 ...

  2. Visual Studio安装卸载模板

    Visual Studio中有两种类型的模板:项目模板和项模板 一.已安装模板: 默认情况下,与产品一起安装的模板位于以下位置: ①\<Visual Studio 安装目录>\Common ...

  3. 简单的两数之和再次乱入<< Add Two Numbers >>

    请看题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored ...

  4. ansible quick start

    1. ansible默认开启ControlPersist,也就是持续化ssh的socket连接,以跳过每次task都需要进行主机认证. 2. 但是centos的openssh版本太老了,不支持Cont ...

  5. git——学习笔记(二)远程仓库

    GIT杀手锏之一——远程仓库 拥有远程仓库的两个办法 1:搭一个Git服务器  2:在GitHub上免费托管的Git仓库 本地仓库   远程仓库 一.在GitHub上免费托管的Git仓库 电脑: 1. ...

  6. Tomcat的安装(一)

    一.Tomcat文件下载类型 1.tar.gz 文件是linux的安装包 2.exe文件是Windows系统的安装包 3.zip文件是Windows系统下压缩版(解压缩即可,免安装) 二.下面使用zi ...

  7. XML元素和结点的区别

    1.区别介绍 Element是Node的扩展,所以也更实用一些.例如,用Element可以方便的获得Node的属性getAttribute(String attrName)如果用Node,可以得到一个 ...

  8. 未来WEB程序员

    作为一名程序员,如果你想在这个领域内继续向前进步或者在当前的经济形势下保持不被炒鱿鱼,那么你就决不应当自满自足,你需要继续学习.近日,著名IT评论员Justin James在他的博客中列出了未来五年程 ...

  9. IOS @2X.png

    [UIImage imageNamed:@"xxx.png"] 或者xib里iPhone4会自动找*@2x.png initWithContentOfFile:pathToImag ...

  10. 关于VS2015找不到WIN32的解决办法

    问题: 原配的Visual Studio 2015专业版不像之前的版本,在新建项目里面是找不到Win32模板的,那么怎么才能新建Win32项目和Win32控制台应用程序呢?先看如下图所示: 解决: 1 ...