[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 ...
随机推荐
- j.u.c系列(02)---线程池ThreadPoolExecutor---tomcat实现策略
写在前面 本文是以同tomcat 7.0.57. jdk版本1.7.0_80为例. 线程池在tomcat中的创建实现为: public abstract class AbstractEndpoint& ...
- [置顶] android socket 聊天实现与调试
网上很多基于Socket的聊天实现都是不完整的... 结合自己的经验给大家分享一下,完整代码可以在GitHub里获取https://github.com/zz7zz7zz/android-socket ...
- Revit Family API 添加材质参数设置可见性
start //添加类型 void AddType(FamilyManager familyMgr, string name, double w, double d) { FamilyType ...
- delphi 文件查找
FindFirst 是用来寻找目标目录下的第一个文件, FindFirst函数在delphi帮助下的定义: function FindFirst(const Path: string; Attr: ...
- After 2 years, I have finally solved my "Slow Hyper-V Guest Network Performance" issue. I am ecstatic.
Edit - It should be known that I was initially researching this issue back in 2012 and the solution ...
- OLE文件拖放
使用IDropTarget接口同时支持文本和文件拖放 关于Windows的外壳扩展编程,拖放是比较简单的一种,在网上可以找到不少介绍这个技巧的文章.大部分是介绍使用MFC的COleDropTarget ...
- Android:intent的基础
只有一个活动的应用也太简单了吧?没错,你的追求应该更高一点.不管你想创建多少 个活动,方法都和上一节中介绍的是一样的.唯一的问题在于,你在启动器中点击应用的图 标只会进入到该应用的主活动,那么怎样才能 ...
- Java知识回顾 (3)运算符
位运算符 Java定义了位运算符,应用于整数类型(int),长整型(long),短整型(short),字符型(char),和字节型(byte)等类型. 位运算符作用在所有的位上,并且按位运算.假设a ...
- ASIHTTPRequestErrorDomain Code=5
ASIHttpRequest解析带空格的URL时 出错!!!(已解决) 用的是post请求 URL 地址是: http://111.234.51.56/login_member.pl?time=201 ...
- 【Devops】【docker】【CI/CD】2.docker启动jenkins环境+安装必要的插件
[注意:]jenkins的docker镜像,需要从官网进入直接获取,其他地方获取到的docker镜像,可能因为Jenkins版本过低,导致后续插件安装失败等问题!!! ================ ...