[leetcode]Clone Graph @ Python
原题地址: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的更多相关文章
- LeetCode: Clone Graph 解题报告
Clone GraphClone an undirected graph. Each node in the graph contains a label and a list of its neig ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- [LeetCode] Clone Graph 克隆无向图
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- LeetCode:Clone Graph
题目如下:实现克隆图的算法 题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- 【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 ...
- 21. Clone Graph
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
随机推荐
- 重写(Override) 重载(Overload)
重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重载(Overload)- 参数必须不同 重载(overloadin ...
- Android-RemoteView-桌面小部件
Android-RemoteView-桌面小部件 学习自 <Android开发艺术探索> https://developer.android.google.cn/guide/topics/ ...
- PC端meta标签
下面介绍meta标签的几个属性,charset,content,http-equiv,name. 一.charset 此特性声明当前文档所使用的字符编码,但该声明可以被任何一个元素的lang特性的值覆 ...
- 关于ListView中EditText在软键盘弹出后的焦点问题
转自:http://www.cnblogs.com/haofei/p/3305030.html 在ListView中,每次弹出软键盘后就会重新调用getView()方法,导致EditText失去焦点. ...
- SNOI 滚粗记
连睡觉都只能睡一半就吓醒 真的蠢 CE了四道 没有cstring 踏马本机怎么能过??!! 还有几次夏令营什么的 可能水水就结束了 最单纯的拿点优惠的想法也没实现 都说以后会有用的 大概是吧 也大概是 ...
- 喵哈哈村的魔法考试 Round #7 (Div.2) 题解
喵哈哈村的魔法考试 Round #7 (Div.2) 注意!后四道题来自于周日的hihocoder offer收割赛第九场. 我建了个群:欢迎加入qscoj交流群,群号码:540667432 大概作为 ...
- 用 consul + consul-template + registrator + nginx 打造真正可动态扩展的服务架构
https://mp.weixin.qq.com/s?src=3×tamp=1503654544&ver=1&signature=UcJdgd4vgt*3AR ...
- Visual Studio 2012使用NUnit单元测试实践01,安装NUnit并使用
在Visual Studio 2012中,默认使用Microsoft自带的MS-Test测试框架.但,Visual Studio同样允许使用第三方测试框架,比如NUnit,xUnit,MbUnit,等 ...
- Linux source命令(转)
Linux source命令: 通常用法:source filepath 或 . filepath 功能:使当前shell读入路径为filepath的shell文件并依次执行文件中的所有语句,通常用于 ...
- [转]浅论ViewController的加载 -- 解决 viewDidLoad 被提前加载的问题(pushViewController 前执行)
一个ViewController,一般通过init或initWithNibName来加载.二者没有什么不同,init最终还是要调用initWithNibName方法(除非这个ViewControlle ...