题目来源:

  https://leetcode.com/problems/clone-graph/


题意分析:

  克隆一个无向图。每个节点包括一个数值和它的邻居。


题目思路:

  直接深度拷贝。


代码(python):

  

# Definition for a undirected graph node
# class UndirectedGraphNode(object):
# def __init__(self, x):
# self.label = x
# self.neighbors = [] class Solution(object):
def cloneGraph(self, node):
"""
:type node: UndirectedGraphNode
:rtype: UndirectedGraphNode
"""
if node == None:
return node
d = {}
def dfs(n):
if n in d:
return d[n]
ans = UndirectedGraphNode(n.label)
d[n] = ans
for i in n.neighbors:
ans.neighbors.append(dfs(i))
return ans
return dfs(node)

[LeetCode]题解(python):133-Clone Graph的更多相关文章

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

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

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

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

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

  5. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  6. leetcode 133. Clone Graph ----- java

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

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

  8. 【leetcode】133. Clone Graph

    题目如下: Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph con ...

  9. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  10. Leetcode#133 Clone Graph

    原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...

随机推荐

  1. S3C6410嵌入式应用平台构建(二)

    [2014-4/11~4/14]经过之前的实验,对Uboot已经有了大体的了解,前我们已经把led灯给点亮,但这不是我们的根本目的,我们是要进入boot启动,经过两天的分析代码和反复的实验,终于可以进 ...

  2. [置顶] How to compile openjdk 7 in RHEL5

    1. 为什么要编译openjdk的code? 因为从Eclipse调试JDK的代码时,方法中的局部变量不能显示,这样是因为编译JDK时,没有编译成debug版本. 2. RHEL5中自带的开发者JDK ...

  3. 精简JRE的思路初探

    引言: JRE是Java程序赖以执行的基础环境,眼下JRE已经很的庞大;即使为了执行一个简单的Hello World的程序.可能依旧须要依赖整个JRE,将近百兆大小的依赖性. 能否够对特定Java程序 ...

  4. SQL学习之高级联结(自联结、自然联结、外联接)

    create table Customers( Id ,), Company ) null, Name ) null ) insert into Customers values('Fun4All', ...

  5. 红豆带你从零学C#系列之:初识继承与多态

    继承 现实生活当中,人类又可以根据职业分为:教师,学生,理发师,售货员 又比如飞机又有种类之分:直升飞机.客机.货机.战斗机等 在程序里面我们可能会通过创建类来描述这样的事物,比如学生类.教师类.理发 ...

  6. Clamp函数

    Clamp函数可以将随机变化的数值限制在一个给定的区间[min, max]内: template<class T> T Clamp(T x, T min, T max) { if (x & ...

  7. PHP无法获取Referer问题排查

    测试结果: 同一个页面,2次打开,第一次能获取到Referer第二次获取不到,很好奇原因所在. test1.php代码是: <?php echo '测试来源:直接载入页面<br/>' ...

  8. FZU Problem 1686 神龙的难题 重复覆盖

    题目链接 给出大矩形的长宽, 矩形里面有1,0两个值, 给出小矩形的长宽, 求用最少的小矩形覆盖所有的1. 重复覆盖的模板题. #include <iostream> #include & ...

  9. RedHat升级内核成功

    升级前 uname -aLinux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x8 ...

  10. 脑波设备mindwaveTGC接口示例

    TGC是一个后台应用程序,它负责和脑波设备建立连接,并获取数据,另一方面,它打开了一个端口在监听,让二次开发的应用程序,可以通过socket连接到这个TGC后台程序,获取脑波数据并展示,这种接口适合非 ...