【leetcode】Clone Graph(python)
类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点,接着訪问第一个邻接点,再訪问邻节点的邻节点。。。。
class Solution:
# @param node, a undirected graph node
# @return a undirected graph node
def cloneGraph(self, node):
if None == node: return None
nodeMap = {}
return self.cloneNode(node, nodeMap) def cloneNode(self, node, nodeMap):
if None == node:
return None
#訪问当前点,这里不是简单的print,而是复制,若已经复制,则返回副本
if nodeMap.has_key(node):
return nodeMap[node]
#若没有副本,则复制一份,相同处理其邻接点
else:
clone = UndirectedGraphNode(node.label)
nodeMap[node] = clone
#訪问其邻居节点
for neighbor in node.neighbors:
clone.neighbors.append(self.cloneNode(neighbor, nodeMap))
return clone
与这个题类似,
Copy List with Random Pointer
这个题目中假设同意使用额外的空间,我们也能够用这样的办法来获得一份拷贝。
class Solution:
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
if None == head: return None
nodeMap = {}
return self.copyListNode(head, nodeMap) def copyListNode(self, node, nodeMap):
if None == node: return None
if nodeMap.has_key(node):
return nodeMap[node]
else:
cpNode = RandomListNode(node.label)
nodeMap[node] = cpNode
cpNode.next = self.copyListNode(node.next, nodeMap)
cpNode.random = self.copyListNode(node.random, nodeMap)
return cpNode
【leetcode】Clone Graph(python)的更多相关文章
- 【LeetCode】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...
- 【leetcode】sort list(python)
链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 ...
- 【leetcode】Word Break(python)
思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】未分类(tag里面没有)(共题)
[334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
随机推荐
- iOS: NSMutableArray的方法removeObject:inRange:
- (void)removeObject:(id)anObject inRange:(NSRange)aRange
- [Ext.Net]GridPanel之Access数据库分页显示
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Sliverlight之 故事板
见Project19 (1) 将一张图片每隔一秒旋转72度,看看效果是什么样(使用定时器) 说明:前端 <Image.RenderTransform> <RotateTransfor ...
- 破解win2008r2服务器域用户名
启动PE系统 进入 cmd窗口 cd 进入 win2008r2服务器的安装盘(假设为d:) d: cd windows/system32 ren osk.exe osk02.exe #重命令屏幕键盘 ...
- Android学习路径(十)如何将Action Bar堆放在布局
默认情况下,action bar出如今activity窗体的顶部,稍微降低了activity布局的总空间.假设你想隐藏或者显示action bar,在这堂用户体验的课程中,你能够通过调用hide() ...
- iOS结合导航控制器和标签栏控制器
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name=& ...
- Heritage from father
Problem Description Famous Harry Potter,who seemd to be a normal and poor boy,is actually a wizard.E ...
- CSA 第五届研讨会 想象
参加第五届CSA云安全联盟研讨会. 人们太.所以,我们没有找到座位.立一个很长的时间.为了弥补没有时间坐在办公室.一个补回来.首先为大家介绍的信贷云,事实上独立: 信-使用-云 1. 信-使用-云 什 ...
- Base64加密解密原理以及代码实现
1. Base64使用A--Z,a--z,0--9,+,/ 这64个字符. 2. 编码原理:将3个字节转换成4个字节( (3 X 8) = 24 = (4 X 6) )先读入3个字节,每读一个字 ...
- 如何在 Swift 中优雅地处理 JSON
阅读目录 在Swift中使用JSON的问题 开始 基础用法 枚举(Enumeration) 下标(Subscripts) 打印 调试与错误处理 后记 因为Swift对于类型有非常严格的控制,它在处 ...