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. checkbox的全选、反选、删除(MainActivity)

    package com.example.ay; import java.util.ArrayList;import java.util.List; import com.example.adapter ...

  2. Qt之控件美化

    级联样式表 (CSS) 包含应用于网页中的元素的样式规则.CSS 样式定义元素的显示方式以及元素在页中的放置位置.可以创建一个通用规则,只要 Web 浏览器遇到一个元素实例,或遇到一个分配给某个特定样 ...

  3. C# IList<T>转为DataTable

    public class WebUtil { /// <summary> /// 转换IList<T>为DataTable/// </summary> /// &l ...

  4. Python开发入门与实战1-开发环境

    1.搭建Python Django开发环境 1.1.Python运行环境安装 Python官网:http://www.python.org/ Python最新源码,二进制文档,新闻资讯等可以在Pyth ...

  5. PHP四舍五入精确小数位及取整

    php中取小数位的函数有sprintf,ceil,floor,round等等函数来实现四舍五入,下面我们就一起来看看具体的实例吧.     本篇文章将使用php对数字进行四舍五入保留N位小数,以及使用 ...

  6. D.T SOFTWARE (1) 软件架构直播答疑课程

    今晚的d.t课程 1项目需求 PPTP服务搭建完成PPTP服务器的搭建,用户重新拨号获得新IP后,要求拔PPTP VPN成功时,也获取到新的公网IP,而且能通过代理上网.VNC服务安装用户可以通过VN ...

  7. Python闭包实现的计数器

    #!/usr/bin/env python #coding=utf-8 def generate_counter(): CNT = [0] def add_one(): CNT[0] = CNT[0] ...

  8. catch的执行与try的匹配

    这里有一段代码: public class EmbededFinally { public static void main(String args[]) { int result; try { Sy ...

  9. (转)mysql各个主要版本之间的差异

    原文:http://blog.csdn.net/z1988316/article/details/8095407   一.各版本的常用命令差异 show innodb status\G mysql-5 ...

  10. postgreSQL环境搭建

    一.安装 操作系统:windows7 安装介质:postgresql-9.1.3-1-windows.exe 二.psql控制台简单使用 1打开psql 2根据提示运行help 3列出表命令 三.安装 ...