【leetcode】133. Clone Graph
题目如下:
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a
label(int) and a list (List[UndirectedGraphNode]) of itsneighbors. There is an edge between the given node and each of the nodes in its neighbors.OJ's undirected graph serialization (so you can understand error output):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
/ \
\_/Note: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer. You don't need to understand the serialization to solve the problem.
解题思路:深拷贝node。题目本身不难,由于所有节点的lable都是唯一的,因此需要保持已经创建过节点的lable,避免出现重复创建。另外节点存在self-cycle,所以遍历过的路径也需要保存。
代码如下:
# Definition for a undirected graph node
class UndirectedGraphNode:
def __init__(self, x):
self.label = x
self.neighbors = [] class Solution:
# @param node, a undirected graph node
# @return a undirected graph node
def cloneGraph(self, node):
if node == None:
return None
root = UndirectedGraphNode(node.label)
queue = [(node,root)]
dic = {}
dic[root.label] = root
dic_visit = {}
while len(queue) > 0:
n,r = queue.pop(0)
if n.label in dic_visit:
continue
for i in n.neighbors:
if i.label not in dic:
i_node = UndirectedGraphNode(i.label)
dic[i.label] = i_node
else:
i_node = dic[i.label]
r.neighbors.append(i_node)
queue.append((i, r.neighbors[-1]))
dic_visit[n.label] = 1 return root
【leetcode】133. Clone Graph的更多相关文章
- 【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】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【Lintcode】137.Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- 【LeetCode】133. 克隆图
133. 克隆图 知识点:图:递归;BFS 题目描述 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[No ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
随机推荐
- zabbix自带database monitor
1. 在zabbix服务器上安装一下两个包: # yum -y install unixODBC mysql-connector-odbc 2. 修改zabbix服务器上ODBC配置: 2.1 Vim ...
- ambari安装hadoop
前言 hadoop除了社区版之外,还有两个主流的分支,一个是cdh版本,一个是hdp版本,其中ambari是hdp版本的部署工具 1.ambari安装准备 https://docs.hortonwor ...
- 工具类Collections、Arrays(传入的是数组)
Collections类: 1. Collections.sort(list) //对list集合进行排序前提是 list里面存储的对象已经实现了 comparable接口 2. Collecti ...
- 20150127 学军集训 day1
day1 就直接考试... 和说好的不一样啊 第一题看都没怎么看就pass了,构造的题我一向没什么把握.然后瞟到第三题有30分可做,虽然要写的代码很大...反正我是写习惯了..期间纠结了一会还写了一个 ...
- POJ 2808 校门外的树(线段树入门)
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...
- jdbc baseDAO 以及 每个类的继承
首先是baseDAO,用来作为DAO的父类 package dao; import java.lang.reflect.Field; import java.sql.Connection; impor ...
- c++ 获取文件图标,类型名称,属性 SHGetFileInfo
SHGetFileInfo是一个相当实用的Windows API函数. // [MoreWindows工作笔记4] 获取文件图标,类型名称,属性 SHGetFileInfo #include < ...
- 【Java架构:基础技术】一篇文章搞掂:Gradle
前言 Gradle是什么: Gradle提供了一个域特定语言(DSL),用于描述构建 它使用 Groovy 语言,使其更容易来形容和构建 Gradle 中的每一个构建脚本使用UTF-8进行编码保存,并 ...
- 8086汇编和Win32汇编
8086汇编是指在某环境下汇编编译产生的程序,用机器去执行每条指令的长度为16位(可小于16),如DOS操作系统:WIN32汇编是32位环境下的汇编,如Windows(Windows也有64位的,XP ...
- Angularjs实现简单的登陆框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...