【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),布布扣,bubuko.com

时间: 2024-10-18 19:24:17

【leetcode】Clone Graph(python)的相关文章

【leetcode】Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

【LeetCode】Clone Graph 解题报告

[题目] Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each

【LeetCode】Clone Graph (2 solutions)

Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label an

【Leetcode】 Clone Graph

题目链接:https://leetcode.com/problems/clone-graph/ 题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node

【leetcode】sort list(python)

链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 == None: return head1 # head1 and head2 point to the same link list if head1 == head2: return head1 head = None tail = None # the small pointer point

【leetcode】Word Break(python)

思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符開始找连续的子串,可是这时与第一个就稍有不同.比方说word='ab', dict={ 'a', ab', ...},在找到a后,接下来处理的是b.我们发现b不在dict中,可是我们发现b能够和a结合,形成ab,而ab在dict中.所以这里的每一个子串就能够有三种选择.要么自己单独作为

【LeetCode】- Two Sum(两数相加)

[ 问题: ] Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

【leetcode】Linked List Cycle (python)

题目分析见这里 class Solution: # @param head, a ListNode # @return a list node def detectCycle(self, head): if None == head or None == head.next: return None pfast = head pslow = head #找第一次相遇的点,若存在环路,则肯定会相遇 while pfast and pfast.next: pfast = pfast.next.nex

【LeetCode】未分类(tag里面没有)(共题)

[419]Battleships in a Board (2018年11月25日)(谷歌的题,没分类.) 给了一个二维平面,上面有 X 和 . 两种字符. 一行或者一列连续的 X 代表一个战舰,问图中有多少个战舰.(题目要求one pass, 空间复杂度是常数) 题目说了每两个战舰之间起码有一个 . 作为分隔符,所以不存在正好交叉的情况. 题解:我提交了一个 floodfill 的题解,能过但是显然不满足要求. discuss里面说,我们可以统计战舰的最上方和最左方,从而来统计战舰的个数.(这个