摘要: 算法思路:BFS 参考:https://leetcode.com/problems/clone-graph/discuss/440722/Python-3-BFS 阅读全文
posted @ 2019-12-11 14:04 Sempron2800+ 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution: 2 def singleNumber(self, nums: 'List[int]') -> int: 3 dic = {} 4 for n in nums: 5 if n not in dic: 6 dic[n] = 1 7 else: 8 dic[n] += 1 9 for k,v in dic.items(): 10 if v == 1: 11 retur 阅读全文
posted @ 2019-12-11 13:59 Sempron2800+ 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 初始化dp所有单元格为maxsize值,定义dp[1][1]表示第一层第一列,是三角中的顶部元素2。 从第二行开始,每个单元格取其左上角和正上方两个单元格中的较小的值,再加上当前节点的值。 则最后一行中的最小值,即为所求。 阅读全文
posted @ 2019-12-11 13:53 Sempron2800+ 阅读(170) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution: 2 def __init__(self): 3 self.lists = [] 4 5 def preOrder(self,root,path): 6 if root != None: 7 path.append(str(root.val)) 8 ... 阅读全文
posted @ 2019-12-11 13:16 Sempron2800+ 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 先找到链表的中间节点,然后从中间节点将链表一分为二。 将后半部分的节点倒序。然后轮流从两个链表中选择节点连接在一起。 参考:https://leetcode.com/problems/reorder-list/discuss/447242/python-solution 阅读全文
posted @ 2019-12-11 13:04 Sempron2800+ 阅读(139) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution: 2 def constructLink(self,lists): 3 n = len(lists) 4 if n == 0: 5 return None 6 if n == 1: 7 return ListNode(lists[0]) 8 ... 阅读全文
posted @ 2019-12-11 12:54 Sempron2800+ 阅读(114) 评论(0) 推荐(0) 编辑
摘要: 1 class Solution: 2 def findRepeatedDnaSequences(self, s: str) -> 'List[str]': 3 n = len(s) 4 if n 1: 20 res.append(k) 21 return res 阅读全文
posted @ 2019-12-11 12:34 Sempron2800+ 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 本题和leetcode116的区别是,116题是完全二叉树,本题是普通二叉树(不一定是完全二叉树)。 但当初做116题的时候,并没有使用完全二叉树这个条件,因此当初的解决方案,完全适用于本题。 阅读全文
posted @ 2019-12-11 11:56 Sempron2800+ 阅读(165) 评论(0) 推荐(0) 编辑
摘要: 本题与leetcode105是同一类的问题。 leetcode105是使用前序和中序构建二叉树,本题是使用中序和后序构建二叉树。 注意,前、中、后三种序列中,只需中序和另外任意一种序列,即可构建二叉树。但是只有前序和后序是无法唯一确定二叉树结构的。 阅读全文
posted @ 2019-12-11 11:27 Sempron2800+ 阅读(217) 评论(0) 推荐(0) 编辑
摘要: 二叉树层次遍历,最后返回每一层的最后一个节点组成的集合。 阅读全文
posted @ 2019-12-11 11:09 Sempron2800+ 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 本题是leetcode112的升级版,需要检查多条路径,并保存。 阅读全文
posted @ 2019-12-11 10:31 Sempron2800+ 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 1 class BSTIterator: 2 def __init__(self, root: TreeNode): 3 self.lists = [] 4 self.idx = 0 5 self.size = 0 6 self.inOrder(root) 7 8 def inOrder(self,root): 9 if root != None: 10 if root.left != None: 阅读全文
posted @ 2019-12-11 10:14 Sempron2800+ 阅读(152) 评论(0) 推荐(0) 编辑
摘要: 这是一道数学题,参考:https://leetcode.com/problems/gray-code/discuss/445279/Python-solution 不了解这个数学原理的,面试时很难做出来。 阅读全文
posted @ 2019-12-11 10:03 Sempron2800+ 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 流程分析,以(4,9)为例: 4 * 3 * 2 * 1 = 24 第一位数字的选择按 3 * 2 * 1 = 6 个为一组,共24个,则分4组1-6 第一组7-12 第二组13-18 第三组19-24 第四组被除数为9,除数为69 // 6 = 19 % 6 = 3 > 0 因此9 是第二组在 1 阅读全文
posted @ 2019-12-11 08:47 Sempron2800+ 阅读(172) 评论(0) 推荐(0) 编辑