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. Activity的任务栈Task以及启动模式与Intent的Flag详解

    什么是任务栈(Task) 官方文档是这么解释的 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即“返回栈”)中. 其实就是以栈的 ...

  2. NYOJ 1107 最高的奖励(贪心+优先队列)

    最高的奖励 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 请问:挖掘机技术哪家强?AC了告诉你! 给你N(N<=3*10^4)个任务,每个任务有一个截止完成时 ...

  3. Oracle 安装中遇到的问题

    第一次用甲骨文,这期待!虽然mySQL也是甲骨文的. 去官网下了Oracle G11 R2 X64,本人的电脑是64位的win7,没开防火墙. 按照网上众多的教程,做完安装,可是安装过程不是那么的顺利 ...

  4. JAVA中的finalize()方法

    [转]JAVA中的finalize()方法 今天早上看Thinking in java的[第四章 初始化和清除].[  清除:终结和垃圾回收]的时候, 看到了这个东西. 用于清理滴... 当然,这个方 ...

  5. birt报表中使用多个数据集。

    这个问题困扰了几天,也没搜到答案,由于工作需要,创建了两个数据集和两个表格,第一个数据集和表格之间没有任何问题.但是第二个数据集拖过去就显示不可用,除非拖到表格外面,当然也就没用了.一朋友说拖一个网格 ...

  6. 使用Maven构建javaWeb项目时,启动tomcat出错:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.conte

    在初学使用maven构建javaWeb的项目的时候,启动tomcat加载时,总是提示如下错误,辛苦一番终于找到解决办法. 严重: Error configuring application liste ...

  7. Qt信号槽连接在有默认形参下的情况思考

    写下这个给自己备忘,比如函数 ) 你在调用端如论是test(3)或者test(),都可以正确调用到这个函数. 但是,如果放到Qt中的信号槽的话,这个还是值得讲一讲的,不然的话,可能会引起相应的误会. ...

  8. Ajax的原理和运行机制

    关于ajax,是最近炒得非常火的一种技术,并且时下它也是非常流行.当然,它并不是什么新技术,而是在各种已有的技术和支持机制下的一个统一.在我的项目中,偶尔也会用到ajax,用来给用户一些无刷新的体验. ...

  9. 初涉JavaScript模式 (4) : 构造函数

    什么是构造函数? 构造函数 是一种特殊的方法 主要用来在创建对象时初始化对象 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 特别的一个类可以有多个构造函数 可根据其参数个数的不 ...

  10. K-means聚类

    聚类算法,无监督学习的范畴,没有明确的类别信息. 给定n个训练样本{x1,x2,x3,...,xn} kmeans算法过程描述如下所示: 1.创建k个点作为起始质心点,c1,c2,...,ck 2.重 ...