From 9b07639364f2586815b10fcec5cf209b169241a8 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sat, 7 Jan 2023 14:08:19 -0600 Subject: [PATCH 1/6] fix minor code style --- problems/0151.翻转字符串里的单词.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index a12d8f76..dc12ae23 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -442,7 +442,7 @@ class Solution: while left <= right and s[left] == ' ': #去除开头的空格 left += 1 while left <= right and s[right] == ' ': #去除结尾的空格 - right = right-1 + right -= 1 tmp = [] while left <= right: #去除单词中间多余的空格 if s[left] != ' ': From a008a740694b4fd99a307dab9e05a1f9276605f2 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 8 Jan 2023 08:21:42 -0600 Subject: [PATCH 2/6] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/前序/vim.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/前序/vim.md b/problems/前序/vim.md index f84e70ac..5c5910d2 100644 --- a/problems/前序/vim.md +++ b/problems/前序/vim.md @@ -66,7 +66,7 @@ IDE那么很吃内存,打开个IDE卡半天,用VIM就很轻便了,秒开 ## 安装 -PowerVim的安防非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。 +PowerVim的安装非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。 安装过程非常简单: ```bash From aad6b8cee27afa237dca3997fa720705410eac7a Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 8 Jan 2023 16:37:47 -0600 Subject: [PATCH 3/6] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BA=86python=20version?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E3=80=82=E5=81=9A=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=E7=AE=80=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 2 +- ...38.把二叉搜索树转换为累加树.md | 29 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 406242a7..28425281 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -23,7 +23,7 @@ * 111.二叉树的最小深度 -![我要打十个](https://tva1.sinaimg.cn/large/008eGmZEly1gnadnltbpjg309603w4qp.gif) +![我要打十个](https://tva1.sinaimg.cn/large/008eGmZEly1gPnadnltbpjg309603w4qp.gif) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index ec12c525..d2e98dc8 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -209,29 +209,28 @@ class Solution { # self.right = right class Solution: def __init__(self): - self.pre = TreeNode() + self.count = 0 def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if root == None: + return ''' 倒序累加替换: - [2, 5, 13] -> [[2]+[1]+[0], [2]+[1], [2]] -> [20, 18, 13] ''' - self.traversal(root) - return root - - def traversal(self, root: TreeNode) -> None: - # 因为要遍历整棵树,所以递归函数不需要返回值 - # Base Case - if not root: - return None - # 单层递归逻辑:中序遍历的反译 - 右中左 - self.traversal(root.right) # 右 + # 右 + self.convertBST(root.right) + # 中 # 中节点:用当前root的值加上pre的值 - root.val += self.pre.val # 中 - self.pre = root + self.count += root.val - self.traversal(root.left) # 左 + root.val = self.count + + # 左 + self.convertBST(root.left) + + return root + ``` ## Go From a8339121e6f98e9b5241169aef93a04b7ca2ad87 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Wed, 11 Jan 2023 13:08:06 -0600 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index eec471af..0a85d04a 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -148,7 +148,7 @@ class Solution { ### Python ```python class Solution: - # 思路1:优先考虑胃饼干 + # 思路1:优先考虑小胃口 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() @@ -160,7 +160,7 @@ class Solution: ``` ```python class Solution: - # 思路2:优先考虑胃口 + # 思路2:优先考虑大胃口 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() From 91f3da4b43f44d60a0f7c60436ec30869788ec04 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:53:05 -0600 Subject: [PATCH 5/6] add Python solution --- ...序与后序遍历序列构造二叉树.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index f8109f85..c2b2872b 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -397,6 +397,9 @@ public: }; ``` +## Python + + # 105.从前序与中序遍历序列构造二叉树 [力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) @@ -650,6 +653,37 @@ class Solution { ``` ## Python +```python +class Solution: + def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: + # 第一步: 特殊情况讨论: 树为空. 或者说是递归终止条件 + if not postorder: + return + + # 第二步: 后序遍历的最后一个就是当前的中间节点 + root_val = postorder[-1] + root = TreeNode(root_val) + + # 第三步: 找切割点. + root_index = inorder.index(root_val) + + # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. + left_inorder = inorder[:root_index] + right_inorder = inorder[root_index + 1:] + + # 第五步: 切割postorder数组. 得到postorder数组的左,右半边. + # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. + left_postorder = postorder[:len(left_inorder)] + right_postorder = postorder[len(left_inorder): len(postorder) - 1] + + + # 第六步: 递归 + root.left = self.buildTree(left_inorder, left_postorder) + root.right = self.buildTree(right_inorder, right_postorder) + + # 第七步: 返回答案 + return root +``` 105.从前序与中序遍历序列构造二叉树 From 1a44d7e7dfd032cf13a42ee4998b6445077c5ebb Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 15 Jan 2023 16:34:26 -0600 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ade84773..192e58a6 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -88,7 +88,7 @@ public: * 情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的 * 情况二:rest[i] = gas[i]-cost[i]为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。 -* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能这个负数填平,能把这个负数填平的节点就是出发节点。 +* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能把这个负数填平,能把这个负数填平的节点就是出发节点。 C++代码如下: