Merge pull request #1853 from Logenleedev/my-contribution

fix minor code style + 错别字
This commit is contained in:
程序员Carl
2023-01-19 10:40:49 +08:00
committed by GitHub
7 changed files with 55 additions and 20 deletions

View File

@ -23,10 +23,12 @@
* 111.二叉树的最小深度
![我要打十个](https://code-thinking.cdn.bcebos.com/gifs/%E6%88%91%E8%A6%81%E6%89%93%E5%8D%81%E4%B8%AA.gif)
# 102.二叉树的层序遍历
[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/)

View File

@ -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.从前序与中序遍历序列构造二叉树

View File

@ -88,7 +88,7 @@ public:
* 情况一如果gas的总和小于cost总和那么无论从哪里出发一定是跑不了一圈的
* 情况二rest[i] = gas[i]-cost[i]为一天剩下的油i从0开始计算累加到最后一站如果累加没有出现负数说明从0出发油就没有断过那么0就是起点。
* 情况三如果累加的最小值是负数汽车就要从非0节点出发从后向前看哪个节点能这个负数填平能把这个负数填平的节点就是出发节点。
* 情况三如果累加的最小值是负数汽车就要从非0节点出发从后向前看哪个节点能这个负数填平,能把这个负数填平的节点就是出发节点。
C++代码如下:

View File

@ -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] != ' ':

View File

@ -173,7 +173,7 @@ class Solution {
### Python
```python
class Solution:
# 思路1优先考虑胃饼干
# 思路1优先考虑小胃口
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
@ -185,7 +185,7 @@ class Solution:
```
```python
class Solution:
# 思路2优先考虑胃口
# 思路2优先考虑胃口
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()

View File

@ -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

View File

@ -66,7 +66,7 @@ IDE那么很吃内存打开个IDE卡半天用VIM就很轻便了秒开
## 安装
PowerVim的安非常简单我已经写好了安装脚本只要执行以下就可以安装而且不会影响你之前的vim配置之前的配置都给做了备份大家看一下脚本就知道备份在哪里了。
PowerVim的安非常简单我已经写好了安装脚本只要执行以下就可以安装而且不会影响你之前的vim配置之前的配置都给做了备份大家看一下脚本就知道备份在哪里了。
安装过程非常简单:
```bash