原题地址:https://oj.leetcode.com/problems/clone-graph/

题意:实现对一个图的深拷贝。

解题思路:由于遍历一个图有两种方式:bfs和dfs。所以深拷贝一个图也可以采用这两种方法。不管使用dfs还是bfs都需要一个哈希表map来存储原图中的节点和新图中的节点的一一映射。map的作用在于替代bfs和dfs中的visit数组,一旦map中出现了映射关系,就说明已经复制完成,也就是已经访问过了。

dfs代码:

# 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
# @BFS
def cloneGraph(self, node):
def dfs(input, map):
if input in map:
return map[input]
output = UndirectedGraphNode(input.label)
map[input] = output
for neighbor in input.neighbors:
output.neighbors.append(dfs(neighbor, map))
return output
if node == None: return None
return dfs(node, {})

bfs代码:

# 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
# @BFS
def cloneGraph(self, node):
if node == None: return None
queue = []; map = {}
newhead = UndirectedGraphNode(node.label)
queue.append(node)
map[node] = newhead
while queue:
curr = queue.pop()
for neighbor in curr.neighbors:
if neighbor not in map:
copy = UndirectedGraphNode(neighbor.label)
map[curr].neighbors.append(copy)
map[neighbor] = copy
queue.append(neighbor)
else:
# turn directed graph to undirected graph
map[curr].neighbors.append(map[neighbor])
return newhead

[leetcode]Clone Graph @ Python的更多相关文章

  1. LeetCode: Clone Graph 解题报告

    Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...

  2. [LeetCode] Clone Graph 无向图的复制

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

  3. [LeetCode] Clone Graph 克隆无向图

    Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...

  4. LeetCode:Clone Graph

    题目如下:实现克隆图的算法  题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...

  5. [Leetcode Week3]Clone Graph

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

  6. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  7. [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

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

  9. 21. Clone Graph

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

随机推荐

  1. jquery获取系统当前时间

    //判断是否在前面加0function getNow(s) { return s < 10 ? '0' + s: s;} var myDate = new Date(); var year=my ...

  2. BZOJ.1021.[SHOI2008]循环的债务(DP)

    题目链接 不同面额的钞票是可以分开考虑的. ↑其实并不很明白具体(证明?),反正是可以像背包一样去做. f[x][i][j]表示用前x种面额钞票满足 A有i元 B有j元 (C有sum-i-j)所需交换 ...

  3. 关于Android4.X的Alertdialog对话框

    最近在做Android4.0的开发,发现AlertDialog相比较以前有了较大变化,就是在触摸对话框边缘外部,对话框消失 于是研究其父类发现,可以设置这么一条属性,当然必须先AlertDialog. ...

  4. shell脚本调用C语言之字符串切分函数——strtok

    今天上午在写一个需求,要求的比较急,要求当天完成,我大致分析了一下,可以采用从shell脚本中插入一连串的日期,通过调用proc生成的可执行文件,将日期传入后台数据库,在数据库中进行计算.需要切分日期 ...

  5. Webpack 性能优化 (一)(使用别名做重定向)

    前言 Webpack 是 OneAPM 前端技术栈中非常重要的一部分.它非常好用,假设你还不了解它,建议你阅读这篇Webpack 入门指迷,在 OneAPM 我们用它完毕静态资源打包.ES6 代码的转 ...

  6. Jquery DataTable基本使用

    1,首先需要引用下面两个文件 <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css ...

  7. C#写的COM组件注册问题兼论微软Regasm注册的BUG

    工作中自己用C#写了专门读写EXCEL(不需要OFFICE环境,直接读原始文件,速度快)的COM组件,在使用过程中,发现原先的注册程序是有问题的.网上也有同样的网友碰到这个问题,但都没找到合适的解决办 ...

  8. WCF实现多个服务

    本篇体验使用WCF实现2个服务.一个使用HTTP通讯,一个使用TCP通讯. 大致思路是: → 创建WCF服务以及接口,写2个接口→ 实现2个接口→ 为WCF创建一个控制台项目的宿主,配置App.con ...

  9. Windows Phone本地数据库(SQLCE):11、使用LINQ查询数据库(翻译) (转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十一篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...

  10. npm WARN saveError ENOENT: no such file or directory

    转自树之名原文npm WARN saveError ENOENT: no such file or directory解决 我是在安装sequelize时出错的.提示的错误没有保存,类似于参考的文章中 ...