Skip to content

Commit aea3faf

Browse files
authored
Merge branch 'youngyangyang04:master' into patch-4
2 parents b4e2179 + 145b5b0 commit aea3faf

12 files changed

+193
-74
lines changed

problems/0062.不同路径.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,25 @@ var uniquePaths = function(m, n) {
327327
return dp[m - 1][n - 1]
328328
};
329329
```
330+
>版本二:直接将dp数值值初始化为1
331+
```javascript
332+
/**
333+
* @param {number} m
334+
* @param {number} n
335+
* @return {number}
336+
*/
337+
var uniquePaths = function(m, n) {
338+
let dp = new Array(m).fill(1).map(() => new Array(n).fill(1));
339+
// dp[i][j] 表示到达(i,j) 点的路径数
340+
for (let i=1; i<m; i++) {
341+
for (let j=1; j< n;j++) {
342+
dp[i][j]=dp[i-1][j]+dp[i][j-1];
343+
}
344+
}
345+
return dp[m-1][n-1];
346+
347+
};
348+
```
330349

331350

332351
-----------------------

problems/0070.爬楼梯完全背包版本.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989

9090
以上分析完毕,C++代码如下:
91-
```
91+
```CPP
9292
class Solution {
9393
public:
9494
int climbStairs(int n) {

problems/0110.平衡二叉树.md

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ public:
125125
126126
1. 明确递归函数的参数和返回值
127127
128-
参数的话为传入的节点指针,就没有其他参数需要传递了,返回值要返回传入节点为根节点树的深度。
128+
参数:当前传入节点。
129+
返回值:以当前传入节点为根节点的树的高度。
129130
130-
那么如何标记左右子树是否差值大于1呢
131+
那么如何标记左右子树是否差值大于1呢
131132
132133
如果当前传入节点为根节点的二叉树已经不是二叉平衡树了,还返回高度的话就没有意义了。
133134
@@ -136,9 +137,9 @@ public:
136137
代码如下:
137138
138139
139-
```
140+
```CPP
140141
// -1 表示已经不是平衡二叉树了,否则返回值是以该节点为根节点树的高度
141-
int getDepth(TreeNode* node)
142+
int getHeight(TreeNode* node)
142143
```
143144

144145
2. 明确终止条件
@@ -147,31 +148,31 @@ int getDepth(TreeNode* node)
147148

148149
代码如下:
149150

150-
```
151+
```CPP
151152
if (node == NULL) {
152153
return 0;
153154
}
154155
```
155156

156157
3. 明确单层递归的逻辑
157158

158-
如何判断当前传入节点为根节点的二叉树是否是平衡二叉树呢,当然是左子树高度和右子树高度相差
159+
如何判断以当前传入节点为根节点的二叉树是否是平衡二叉树呢?当然是其左子树高度和其右子树高度的差值
159160

160-
分别求出左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉树了
161+
分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉平衡树了
161162

162163
代码如下:
163164

164165
```CPP
165-
int leftDepth = depth(node->left); // 左
166-
if (leftDepth == -1) return -1;
167-
int rightDepth = depth(node->right); // 右
168-
if (rightDepth == -1) return -1;
166+
int leftHeight = getHeight(node->left); //
167+
if (leftHeight == -1) return -1;
168+
int rightHeight = getHeight(node->right); //
169+
if (rightHeight == -1) return -1;
169170

170171
int result;
171-
if (abs(leftDepth - rightDepth) > 1) { // 中
172+
if (abs(leftHeight - rightHeight) > 1) { // 中
172173
result = -1;
173174
} else {
174-
result = 1 + max(leftDepth, rightDepth); // 以当前节点为根节点的最大高度
175+
result = 1 + max(leftHeight, rightHeight); // 以当前节点为根节点的树的最大高度
175176
}
176177

177178
return result;
@@ -180,27 +181,27 @@ return result;
180181
代码精简之后如下:
181182

182183
```CPP
183-
int leftDepth = getDepth(node->left);
184-
if (leftDepth == -1) return -1;
185-
int rightDepth = getDepth(node->right);
186-
if (rightDepth == -1) return -1;
187-
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth);
184+
int leftHeight = getHeight(node->left);
185+
if (leftHeight == -1) return -1;
186+
int rightHeight = getHeight(node->right);
187+
if (rightHeight == -1) return -1;
188+
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
188189
```
189190

190191
此时递归的函数就已经写出来了,这个递归的函数传入节点指针,返回以该节点为根节点的二叉树的高度,如果不是二叉平衡树,则返回-1。
191192

192-
getDepth整体代码如下
193+
getHeight整体代码如下
193194

194195
```CPP
195-
int getDepth(TreeNode* node) {
196+
int getHeight(TreeNode* node) {
196197
if (node == NULL) {
197198
return 0;
198199
}
199-
int leftDepth = getDepth(node->left);
200-
if (leftDepth == -1) return -1;
201-
int rightDepth = getDepth(node->right);
202-
if (rightDepth == -1) return -1;
203-
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth);
200+
int leftHeight = getHeight(node->left);
201+
if (leftHeight == -1) return -1;
202+
int rightHeight = getHeight(node->right);
203+
if (rightHeight == -1) return -1;
204+
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
204205
}
205206
```
206207
@@ -210,18 +211,18 @@ int getDepth(TreeNode* node) {
210211
class Solution {
211212
public:
212213
// 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
213-
int getDepth(TreeNode* node) {
214+
int getHeight(TreeNode* node) {
214215
if (node == NULL) {
215216
return 0;
216217
}
217-
int leftDepth = getDepth(node->left);
218-
if (leftDepth == -1) return -1; // 说明左子树已经不是二叉平衡树
219-
int rightDepth = getDepth(node->right);
220-
if (rightDepth == -1) return -1; // 说明右子树已经不是二叉平衡树
221-
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth);
218+
int leftHeight = getHeight(node->left);
219+
if (leftHeight == -1) return -1;
220+
int rightHeight = getHeight(node->right);
221+
if (rightHeight == -1) return -1;
222+
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
222223
}
223224
bool isBalanced(TreeNode* root) {
224-
return getDepth(root) == -1 ? false : true;
225+
return getHeight(root) == -1 ? false : true;
225226
}
226227
};
227228
```
@@ -498,20 +499,35 @@ class Solution {
498499
## Python
499500

500501
递归法:
501-
```python
502+
```python3
503+
# Definition for a binary tree node.
504+
# class TreeNode:
505+
# def __init__(self, val=0, left=None, right=None):
506+
# self.val = val
507+
# self.left = left
508+
# self.right = right
502509
class Solution:
503510
def isBalanced(self, root: TreeNode) -> bool:
504-
return True if self.getDepth(root) != -1 else False
511+
if self.get_height(root) != -1:
512+
return True
513+
else:
514+
return False
505515

506-
#返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
507-
def getDepth(self, node):
508-
if not node:
516+
def get_height(self, root: TreeNode) -> int:
517+
# Base Case
518+
if not root:
509519
return 0
510-
leftDepth = self.getDepth(node.left)
511-
if leftDepth == -1: return -1 #说明左子树已经不是二叉平衡树
512-
rightDepth = self.getDepth(node.right)
513-
if rightDepth == -1: return -1 #说明右子树已经不是二叉平衡树
514-
return -1 if abs(leftDepth - rightDepth)>1 else 1 + max(leftDepth, rightDepth)
520+
#
521+
if (left_height := self.get_height(root.left)) == -1:
522+
return -1
523+
#
524+
if (right_height := self.get_height(root.right)) == -1:
525+
return -1
526+
#
527+
if abs(left_height - right_height) > 1:
528+
return -1
529+
else:
530+
return 1 + max(left_height, right_height)
515531
```
516532

517533
迭代法:

problems/0206.翻转链表.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ public:
9696
};
9797
```
9898

99+
我们可以发现,上面的递归写法和双指针法实质上都是从前往后翻转指针指向,其实还有另外一种与双指针法不同思路的递归写法:从后往前翻转指针指向。
100+
101+
具体代码如下(带详细注释):
102+
103+
```CPP
104+
class Solution {
105+
public:
106+
ListNode* reverseList(ListNode* head) {
107+
// 边缘条件判断
108+
if(head == NULL) return NULL;
109+
if (head->next == NULL) return head;
110+
111+
// 递归调用,翻转第二个节点开始往后的链表
112+
ListNode *last = reverseList(head->next);
113+
// 翻转头节点与第二个节点的指向
114+
head->next->next = head;
115+
// 此时的 head 节点为尾节点,next 需要指向 NULL
116+
head->next = NULL;
117+
return last;
118+
}
119+
};
120+
```
99121
100122
101123
## 其他语言版本
@@ -135,9 +157,9 @@ class Solution {
135157
temp = cur.next;// 先保存下一个节点
136158
cur.next = prev;// 反转
137159
// 更新prev、cur位置
138-
prev = cur;
139-
cur = temp;
140-
return reverse(prev, cur);
160+
// prev = cur;
161+
// cur = temp;
162+
return reverse(cur, temp);
141163
}
142164
}
143165
```

problems/0257.二叉树的所有路径.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -404,33 +404,41 @@ class Solution {
404404
}
405405
}
406406
```
407-
408-
Python:
409-
```Python
407+
---
408+
Python:
409+
递归法+隐形回溯
410+
```Python3
411+
# Definition for a binary tree node.
412+
# class TreeNode:
413+
# def __init__(self, val=0, left=None, right=None):
414+
# self.val = val
415+
# self.left = left
416+
# self.right = right
410417
class Solution:
411-
"""二叉树的所有路径 递归法"""
412-
413418
def binaryTreePaths(self, root: TreeNode) -> List[str]:
414-
path, result = '', []
419+
path = ''
420+
result = []
421+
if not root: return result
415422
self.traversal(root, path, result)
416423
return result
417424

418-
def traversal(self, cur: TreeNode, path: List, result: List):
425+
def traversal(self, cur: TreeNode, path: str, result: List[str]) -> None:
419426
path += str(cur.val)
420-
# 如果当前节点为叶子节点,添加路径到结果中
421-
if not (cur.left or cur.right):
427+
# 若当前节点为leave,直接输出
428+
if not cur.left and not cur.right:
422429
result.append(path)
423-
return
424-
430+
425431
if cur.left:
432+
# + '->' 是隐藏回溯
426433
self.traversal(cur.left, path + '->', result)
427-
434+
428435
if cur.right:
429436
self.traversal(cur.right, path + '->', result)
430-
431437
```
432-
433-
```python
438+
439+
迭代法:
440+
441+
```python3
434442
from collections import deque
435443

436444

@@ -457,7 +465,8 @@ class Solution:
457465

458466
return result
459467
```
460-
468+
469+
---
461470

462471
Go:
463472
```go
@@ -482,7 +491,7 @@ func binaryTreePaths(root *TreeNode) []string {
482491
return res
483492
}
484493
```
485-
494+
---
486495
JavaScript:
487496

488497
1.递归版本

problems/0279.完全平方数.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ var numSquares1 = function(n) {
334334
let dp = new Array(n + 1).fill(Infinity)
335335
dp[0] = 0
336336

337-
for(let i = 0; i <= n; i++) {
338-
let val = i * i
337+
for(let i = 1; i**2 <= n; i++) {
338+
let val = i**2
339339
for(let j = val; j <= n; j++) {
340340
dp[j] = Math.min(dp[j], dp[j - val] + 1)
341341
}

problems/0392.判断子序列.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public:
141141
142142
143143
Java:
144-
```
144+
```java
145145
class Solution {
146146
public boolean isSubsequence(String s, String t) {
147147
int length1 = s.length(); int length2 = t.length();

problems/0416.分割等和子集.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class Solution:
227227
```
228228
Go:
229229

230-
```
230+
```go
231231
func canPartition(nums []int) bool {
232232
/**
233233
动态五部曲:

problems/0494.目标和.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ const findTargetSumWays = (nums, target) => {
371371
}
372372
373373
const halfSum = (target + sum) / 2;
374-
nums.sort((a, b) => a - b);
375374
376375
let dp = new Array(halfSum+1).fill(0);
377376
dp[0] = 1;

0 commit comments

Comments
 (0)