
LeetCode
caisense
这个作者很懒,什么都没留下…
展开
-
move to gitbook
https://cai-sen-se.gitbook.io/leetcode/原创 2019-06-20 21:15:18 · 134 阅读 · 0 评论 -
132. 分割回文串 II
https://leetcode-cn.com/problems/palindrome-partitioning-ii/comments/回溯(超时):class Solution: def minCut(self, s): """ :type s: str :rtype: int """ self.min...原创 2019-01-24 22:49:08 · 235 阅读 · 0 评论 -
131. 分割回文串
https://leetcode-cn.com/problems/palindrome-partitioning/submissions/class Solution: def partition(self, s): """ :type s: str :rtype: List[List[str]] """ s..原创 2019-01-23 12:27:19 · 146 阅读 · 0 评论 -
130. 被围绕的区域
https://leetcode-cn.com/problems/surrounded-regions/comments/两次遍历。第一次先遍历边界,dfs将所有与边界O相连的O置为‘-’。之后再遍历所有,将所有O变为X,所有’-‘变为Oclass Solution: def solve(self, board): """ :type board: Li...原创 2019-01-22 12:16:13 · 368 阅读 · 0 评论 -
129. 求根到叶子节点数字之和
https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/submissions/# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = ...原创 2019-01-21 12:54:05 · 225 阅读 · 0 评论 -
128. 最长连续序列
https://leetcode-cn.com/problems/longest-consecutive-sequence/用哈希表存储每个端点值对应连续区间的长度若数已在哈希表中:跳过不做处理若是新数加入:取出其左右相邻数已有的连续区间长度 left 和 right计算当前数的区间长度为:cur_length = left + right + 1根据 cur_length 更新最大长...原创 2019-01-20 13:52:14 · 199 阅读 · 0 评论 -
433. 最小基因变化
https://leetcode-cn.com/problems/minimum-genetic-mutation/submissions/基本类似127题,bfsimport queueclass Solution: def minMutation(self, start, end, bank): """ :type start: str ...原创 2019-01-19 17:42:19 · 512 阅读 · 0 评论 -
127. 单词接龙
https://leetcode-cn.com/problems/word-ladder/思路和126差不多,区别在于只用bfs,每次访问一层,用一个map来存储访问过的结点以及与起点的距离,避免重复访问。最后输出map中的终点项即可import queueclass Solution: def ladderLength(self, beginWord, endWord, wordL...原创 2019-01-18 16:37:24 · 170 阅读 · 0 评论 -
126. 单词接龙 II
https://leetcode-cn.com/problems/word-ladder-ii/submissions/dfs+bfsimport queueclass Solution: def findLadders(self, beginWord, endWord, wordList): """ :type beginWord: str ...原创 2019-01-17 18:39:12 · 579 阅读 · 0 评论 -
125. 验证回文串
https://leetcode-cn.com/problems/valid-palindrome/comments/class Solution: def isPalindrome(self, s): """ :type s: str :rtype: bool """ #将s过滤,只保留数字和字母,再转为小...原创 2019-01-08 23:19:44 · 110 阅读 · 0 评论 -
124. 二叉树中的最大路径和
https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/submissions/对于任意一个节点, 如果最大和路径包含该节点, 那么只可能是两种情况:1. 其左右子树中所构成的和路径值较大的那个加上该节点的值后向父节点回溯构成最大路径2. 左右子树都在最大路径中, 加上该节点的值构成了最终的最大路径。# Definiti...原创 2019-01-08 22:44:27 · 167 阅读 · 0 评论 -
123. 买卖股票的最佳时机 III
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/comments/dp1[i] = max(dp[i-1], prices[i] - minval) 从前往后遍历,表示第1天到第i天之间的最大利润(通过是否在第i天卖出确认);dp2[i] = max(dp[i+1], maxval - prices[i]...原创 2019-01-06 16:17:14 · 386 阅读 · 0 评论 -
122. 买卖股票的最佳时机 II
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/submissions/观察知,累加每两个相邻峰谷之间的差价即可class Solution(object): def maxProfit(self, prices): """ :type prices: List[in...原创 2019-01-04 16:42:46 · 114 阅读 · 0 评论 -
121. 买卖股票的最佳时机
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int ...原创 2019-01-03 13:21:39 · 118 阅读 · 0 评论 -
120. 三角形最小路径和
https://leetcode-cn.com/problems/triangle/submissions/dp,自底向上class Solution: def minimumTotal(self, triangle): """ :type triangle: List[List[int]] :rtype: int """...原创 2018-12-30 23:14:51 · 112 阅读 · 0 评论 -
119. 杨辉三角 II
https://leetcode-cn.com/problems/pascals-triangle-ii/class Solution: def getRow(self, rowIndex): """ :type rowIndex: int :rtype: List[int] """ res...原创 2018-12-28 17:55:16 · 123 阅读 · 0 评论 -
118. 杨辉三角
https://leetcode-cn.com/problems/pascals-triangle/class Solution: def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ if numRow...原创 2018-12-28 17:53:08 · 137 阅读 · 0 评论 -
117. 填充同一层的兄弟节点 II
https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/和116一样层序import Queueclass Solution: # @param root, a tree link node # @return nothing def connect(self, r...原创 2018-12-28 17:51:26 · 173 阅读 · 0 评论 -
116. 填充同一层的兄弟节点
https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/comments/# Definition for binary tree with next pointer.# class TreeLinkNode:# def __init__(self, x):# self...原创 2018-12-25 16:32:11 · 117 阅读 · 0 评论 -
114. 二叉树展开为链表
https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/submissions/class Solution: def flatten(self, root): """ :type root: TreeNode :rtype: void Do not retur...原创 2018-12-25 15:51:33 · 129 阅读 · 0 评论 -
113. 路径总和 II
https://leetcode-cn.com/problems/path-sum-ii/思路:回溯法。class Solution: def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]]...原创 2018-12-23 17:53:41 · 220 阅读 · 0 评论 -
112. 路径总和
https://leetcode-cn.com/problems/path-sum/思路:dfs,每次用sum减去当前节点的值,直到sum为0class Solution: def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :r...原创 2018-12-23 16:37:53 · 209 阅读 · 0 评论 -
111. 二叉树的最小深度
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/思路1:dfs递归class Solution: def minDepth(self, root): """ :type root: TreeNode :rtype: int """ ...原创 2018-12-22 23:21:52 · 128 阅读 · 0 评论 -
110. 平衡二叉树
思路:后序遍历,对root的左右子树递归,每次计算root的左右子树高,然后计算root的高,为左右较高者+1.若左右之差大于1,全局flag为false# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self....原创 2018-12-22 17:31:54 · 127 阅读 · 0 评论 -
109. 有序链表转换二叉搜索树
https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/description/思路:用一个全局变量node顺序遍历链表,然后用中序遍历方式递归构造树即可(画图比较好理解)# Definition for singly-linked list.# class ListNode:# def ...原创 2018-10-26 00:21:25 · 251 阅读 · 0 评论 -
108. 将有序数组转换为二叉搜索树
https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/ 思路:递归,根据有序的特性,每次取数组中点mid为根,递归构造左右子树# Definition for a binary tree node.# class TreeNode:# def __init__...原创 2018-09-13 17:54:05 · 251 阅读 · 0 评论 -
107. 二叉树的层次遍历 II
https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/description/ 思路:层序遍历,然后结果倒序# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self...原创 2018-09-13 17:50:42 · 223 阅读 · 0 评论 -
106. 从中序与后序遍历序列构造二叉树
https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 思路:和105题类似,区别在于后序的-1号元素是中序的根,划分之后递归# Definition for a binary tree node.# class TreeNode:# ...原创 2018-09-13 17:49:03 · 141 阅读 · 0 评论 -
105. 从前序与中序遍历序列构造二叉树
https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/思路:递归,易知前序的第0号元素为树的根,在中序里找这个根,然后以该根为界将中序数组划分成左右两半,依次递归进行。难点在于每次还要根据中序的划分来将前序数组也划分。# Definitio...原创 2018-09-13 17:47:15 · 184 阅读 · 0 评论 -
754. 到达终点数字
https://leetcode-cn.com/problems/reach-a-number/description/ 思路:先把target转为正数,因为正负都是对称的。 先累加,若sum==target,直接返回;若sum>target,先计算sum与target的差距bias,若bias为偶数,也直接返回。这里用到一个很巧妙的思路:若bias为偶,则将第bias/2步反向走即可,...原创 2018-08-02 23:36:28 · 780 阅读 · 1 评论 -
拼多多 牛客7.22笔试算法岗 第二题
对一个长度为n的字符串p,我们可以通过p构造出一个无线长度的字符串s,其中 s[i] = p[i%n]。 给定一个字符串s,求可以通过上述方法构造出字符串s的最短p思路:找s中最短的周期字符串。class Solution: def shortestString(self, s): """ :type A: List[int] :...原创 2018-08-02 22:09:24 · 749 阅读 · 0 评论 -
845. Longest Mountain in Array
https://leetcode.com/problems/longest-mountain-in-array/description/ 找最长的山脉 思路:一次遍历,找山顶(比两端都高)。若找到,从山顶向两端发散,求山脉长度,每次更新最大长度。class Solution: def longestMountain(self, A): """ :ty...原创 2018-08-01 23:31:42 · 273 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/ 还是层序遍历,不同的是要求每层的遍历方向不同 思路:还是层序,设一个变量direct,初始为0,每层反转一次,根据direct决定是否将该层数据倒序# Definition for a binary tree node.#...原创 2018-07-16 15:09:28 · 144 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
https://leetcode.com/problems/binary-tree-level-order-traversal/description/ 二叉树层序遍历 思路:经典的队列问题# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.va...原创 2018-07-15 21:22:25 · 301 阅读 · 0 评论 -
101. Symmetric Tree
https://leetcode.com/problems/symmetric-tree/description/ 判断一棵树是否对称思路1:naive的方法。先层序遍历,检查每一层是否对称。此时还不能说明是对称(为什么?见下图) 这棵树层序遍历是“对称”的,但是实际上不对称的所以还要加一次中序遍历,若中序的结果是轴对称的,则符合。注意,若只中序遍历,即使结果轴对称,也不符合,...原创 2018-07-15 21:00:34 · 123 阅读 · 0 评论 -
100. Same Tree
https://leetcode.com/problems/same-tree/description/ 判断两棵二叉树是否相同(结构一样,相同位置结点一样) 思路:按相同顺序遍历,用一个全局flag,初始为true。每当两棵树的结点不相等,或者一棵树有结点而另一棵树为空时,flag=false# Definition for a binary tree node.# class Tre...原创 2018-07-14 23:43:20 · 105 阅读 · 0 评论 -
95. Unique Binary Search Trees II
https://leetcode.com/problems/unique-binary-search-trees-ii/description/ 类似96. Unique Binary Search Trees,不同在于要求所有二叉搜索树形状,而不只是计算种数思路:递归。根据二叉搜索树的性质,在[1…n]中依次选择根i(1 <= i <= n),划分出左([1...i-1])右(...原创 2018-07-14 21:25:14 · 355 阅读 · 0 评论 -
96. Unique Binary Search Trees
https://leetcode.com/problems/unique-binary-search-trees/description/ 给一个数n,求[1..n]能够成二叉搜索树的数目 思路:见discuss。用dpclass Solution: def numTrees(self, n): """ :type n: int :r...原创 2018-07-14 18:01:16 · 139 阅读 · 0 评论 -
98. Validate Binary Search Tree
https://leetcode.com/problems/validate-binary-search-tree/description/ 判断一棵树是否是二叉搜索树 思路:中序遍历,将结果记录在data,再遍历data,看是否是升序,是则符合。# Definition for a binary tree node.# class TreeNode:# def __ini...原创 2018-07-13 21:14:13 · 117 阅读 · 0 评论 -
145. Binary Tree Postorder Traversal
https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 二叉树后序遍历 思路1:递归# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = ...原创 2018-07-13 19:50:28 · 156 阅读 · 0 评论