Skip to content

Commit b013f3d

Browse files
Merge branch 'master' into master
2 parents 6712105 + de3fa62 commit b013f3d

33 files changed

+1317
-228
lines changed

.DS_Store

8 KB
Binary file not shown.

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
> 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
77
> 2. **PDF版本**[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html)
8-
> 3. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。
9-
> 4. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html)
10-
> 5. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。
11-
> 6. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
8+
> 3. **最强八股文:**[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html)
9+
> 4. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。
10+
> 5. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html)
11+
> 6. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。
12+
> 7. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
1213
1314
<p align="center">
1415
<a href="programmercarl.com" target="_blank">

problems/0045.跳跃游戏II.md

+24
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public:
142142

143143
### Java
144144
```Java
145+
// 版本一
145146
class Solution {
146147
public int jump(int[] nums) {
147148
if (nums == null || nums.length == 0 || nums.length == 1) {
@@ -172,7 +173,30 @@ class Solution {
172173
}
173174
```
174175

176+
```java
177+
// 版本二
178+
class Solution {
179+
public int jump(int[] nums) {
180+
int result = 0;
181+
// 当前覆盖的最远距离下标
182+
int end = 0;
183+
// 下一步覆盖的最远距离下标
184+
int temp = 0;
185+
for (int i = 0; i <= end && end < nums.length - 1; ++i) {
186+
temp = Math.max(temp, i + nums[i]);
187+
// 可达位置的改变次数就是跳跃次数
188+
if (i == end) {
189+
end = temp;
190+
result++;
191+
}
192+
}
193+
return result;
194+
}
195+
}
196+
```
197+
175198
### Python
199+
176200
```python
177201
class Solution:
178202
def jump(self, nums: List[int]) -> int:

problems/0063.不同路径II.md

+14-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</a>
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
66

7-
## 63. 不同路径 II
7+
# 63. 不同路径 II
88

99
[力扣题目链接](https://leetcode-cn.com/problems/unique-paths-ii/)
1010

@@ -22,23 +22,22 @@
2222

2323
![](https://img-blog.csdnimg.cn/20210111204939971.png)
2424

25-
输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
26-
输出:2
25+
* 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
26+
* 输出:2
2727
解释:
28-
3x3 网格的正中间有一个障碍物。
29-
从左上角到右下角一共有 2 条不同的路径:
30-
1. 向右 -> 向右 -> 向下 -> 向下
31-
2. 向下 -> 向下 -> 向右 -> 向右
28+
* 3x3 网格的正中间有一个障碍物。
29+
* 从左上角到右下角一共有 2 条不同的路径:
30+
1. 向右 -> 向右 -> 向下 -> 向下
31+
2. 向下 -> 向下 -> 向右 -> 向右
3232

3333
示例 2:
3434

3535
![](https://img-blog.csdnimg.cn/20210111205857918.png)
3636

37-
输入:obstacleGrid = [[0,1],[0,0]]
38-
输出:1
37+
* 输入:obstacleGrid = [[0,1],[0,0]]
38+
* 输出:1
3939

4040
提示:
41-
4241
* m == obstacleGrid.length
4342
* n == obstacleGrid[i].length
4443
* 1 <= m, n <= 100
@@ -171,7 +170,7 @@ public:
171170
172171
## 其他语言版本
173172
174-
Java
173+
### Java
175174
176175
```java
177176
class Solution {
@@ -199,7 +198,7 @@ class Solution {
199198
```
200199

201200

202-
Python
201+
### Python
203202

204203
```python
205204
class Solution:
@@ -262,7 +261,7 @@ class Solution:
262261
```
263262

264263

265-
Go:
264+
### Go
266265

267266
```go
268267
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
@@ -295,8 +294,8 @@ func uniquePathsWithObstacles(obstacleGrid [][]int) int {
295294

296295
```
297296

298-
Javascript
299-
``` Javascript
297+
### Javascript
298+
```Javascript
300299
var uniquePathsWithObstacles = function(obstacleGrid) {
301300
const m = obstacleGrid.length
302301
const n = obstacleGrid[0].length

problems/0096.不同的二叉搜索树.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</a>
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
66

7-
## 96.不同的二叉搜索树
7+
# 96.不同的二叉搜索树
88

99
[力扣题目链接](https://leetcode-cn.com/problems/unique-binary-search-trees/)
1010

@@ -163,7 +163,7 @@ public:
163163
## 其他语言版本
164164
165165
166-
Java
166+
### Java
167167
```Java
168168
class Solution {
169169
public int numTrees(int n) {
@@ -184,7 +184,7 @@ class Solution {
184184
}
185185
```
186186

187-
Python
187+
### Python
188188
```python
189189
class Solution:
190190
def numTrees(self, n: int) -> int:
@@ -196,7 +196,7 @@ class Solution:
196196
return dp[-1]
197197
```
198198

199-
Go:
199+
### Go
200200
```Go
201201
func numTrees(n int)int{
202202
dp:=make([]int,n+1)
@@ -210,7 +210,7 @@ func numTrees(n int)int{
210210
}
211211
```
212212

213-
Javascript
213+
### Javascript
214214
```Javascript
215215
const numTrees =(n) => {
216216
let dp = new Array(n+1).fill(0);

problems/0101.对称二叉树.md

+69
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,75 @@ var isSymmetric = function(root) {
574574
};
575575
```
576576

577+
## TypeScript:
578+
579+
> 递归法
580+
581+
```typescript
582+
function isSymmetric(root: TreeNode | null): boolean {
583+
function recur(node1: TreeNode | null, node2: TreeNode | null): boolean {
584+
if (node1 === null && node2 === null) return true;
585+
if (node1 === null || node2 === null) return false;
586+
if (node1.val !== node2.val) return false
587+
let isSym1: boolean = recur(node1.left, node2.right);
588+
let isSym2: boolean = recur(node1.right, node2.left);
589+
return isSym1 && isSym2
590+
}
591+
if (root === null) return true;
592+
return recur(root.left, root.right);
593+
};
594+
```
595+
596+
> 迭代法
597+
598+
```typescript
599+
// 迭代法(队列)
600+
function isSymmetric(root: TreeNode | null): boolean {
601+
let helperQueue: (TreeNode | null)[] = [];
602+
let tempNode1: TreeNode | null,
603+
tempNode2: TreeNode | null;
604+
if (root !== null) {
605+
helperQueue.push(root.left);
606+
helperQueue.push(root.right);
607+
}
608+
while (helperQueue.length > 0) {
609+
tempNode1 = helperQueue.shift()!;
610+
tempNode2 = helperQueue.shift()!;
611+
if (tempNode1 === null && tempNode2 === null) continue;
612+
if (tempNode1 === null || tempNode2 === null) return false;
613+
if (tempNode1.val !== tempNode2.val) return false;
614+
helperQueue.push(tempNode1.left);
615+
helperQueue.push(tempNode2.right);
616+
helperQueue.push(tempNode1.right);
617+
helperQueue.push(tempNode2.left);
618+
}
619+
return true;
620+
}
621+
622+
// 迭代法(栈)
623+
function isSymmetric(root: TreeNode | null): boolean {
624+
let helperStack: (TreeNode | null)[] = [];
625+
let tempNode1: TreeNode | null,
626+
tempNode2: TreeNode | null;
627+
if (root !== null) {
628+
helperStack.push(root.left);
629+
helperStack.push(root.right);
630+
}
631+
while (helperStack.length > 0) {
632+
tempNode1 = helperStack.pop()!;
633+
tempNode2 = helperStack.pop()!;
634+
if (tempNode1 === null && tempNode2 === null) continue;
635+
if (tempNode1 === null || tempNode2 === null) return false;
636+
if (tempNode1.val !== tempNode2.val) return false;
637+
helperStack.push(tempNode1.left);
638+
helperStack.push(tempNode2.right);
639+
helperStack.push(tempNode1.right);
640+
helperStack.push(tempNode2.left);
641+
}
642+
return true;
643+
};
644+
```
645+
577646
## Swift:
578647

579648
> 递归

0 commit comments

Comments
 (0)