Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6dd7f42

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 06ebd5f + eb86690 commit 6dd7f42

32 files changed

+1147
-393
lines changed

‎README.md‎

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

6767
**这里每一篇题解,都是精品,值得仔细琢磨**
6868

69-
我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)的贡献的代码,当然我也会严格把控代码质量。
69+
我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。
7070

7171
**所以也欢迎大家参与进来,完善题解的各个语言版本,拥抱开源,让更多小伙伴们受益**
7272

‎problems/0028.实现strStr.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ public:
444444
};
445445

446446
```
447+
* 时间复杂度: O(n + m)
448+
* 空间复杂度: O(m), 只需要保存字符串needle的前缀表
447449
448450
# 前缀表(不减一)C++实现
449451
@@ -540,6 +542,9 @@ public:
540542
}
541543
};
542544
```
545+
* 时间复杂度: O(n + m)
546+
* 空间复杂度: O(m)
547+
543548

544549
# 总结
545550

‎problems/0045.跳跃游戏II.md‎

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

7+
> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心里准备!
78
8-
> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心里准备!
9-
10-
# 45.跳跃游戏II
9+
# 45.跳跃游戏 II
1110

1211
[力扣题目链接](https://leetcode.cn/problems/jump-game-ii/)
1312

@@ -18,13 +17,17 @@
1817
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
1918

2019
示例:
21-
* 输入: [2,3,1,1,4]
22-
* 输出: 2
23-
* 解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
20+
21+
- 输入: [2,3,1,1,4]
22+
- 输出: 2
23+
- 解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
2424

2525
说明:
2626
假设你总是可以到达数组的最后一个位置。
2727

28+
# 视频讲解
29+
30+
**《代码随想录》算法视频公开课:[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**
2831

2932
## 思路
3033

@@ -46,7 +49,6 @@
4649

4750
如图:
4851

49-
5052
![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103.png)
5153

5254
**图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)**
@@ -57,8 +59,8 @@
5759

5860
这里还是有个特殊情况需要考虑,当移动下标达到了当前覆盖的最远距离下标时
5961

60-
* 如果当前覆盖最远距离下标不是是集合终点,步数就加一,还需要继续走。
61-
* 如果当前覆盖最远距离下标就是是集合终点,步数不用加一,因为不能再往后走了。
62+
- 如果当前覆盖最远距离下标不是是集合终点,步数就加一,还需要继续走。
63+
- 如果当前覆盖最远距离下标就是是集合终点,步数不用加一,因为不能再往后走了。
6264

6365
C++代码如下:(详细注释)
6466

@@ -92,14 +94,14 @@ public:
9294
9395
**针对于方法一的特殊情况,可以统一处理**,即:移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不考虑是不是终点的情况。
9496
95-
想要达到这样的效果,只要让移动下标,最大只能移动到nums.size - 2的地方就可以了
97+
想要达到这样的效果,只要让移动下标,最大只能移动到 nums.size - 2 的地方就可以了
9698
97-
因为当移动下标指向nums.size - 2时:
99+
因为当移动下标指向 nums.size - 2 时:
98100
99-
* 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图:
100-
![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png)
101+
- 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即 ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图:
102+
![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png)
101103
102-
* 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图:
104+
- 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图:
103105
104106
![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693.png)
105107
@@ -127,7 +129,7 @@ public:
127129

128130
可以看出版本二的代码相对于版本一简化了不少!
129131

130-
**其精髓在于控制移动下标i只移动到nums.size() - 2的位置**,所以移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不用考虑别的了。
132+
**其精髓在于控制移动下标 i 只移动到 nums.size() - 2 的位置**,所以移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不用考虑别的了。
131133

132134
## 总结
133135

@@ -137,11 +139,10 @@ public:
137139

138140
理解本题的关键在于:**以最小的步数增加最大的覆盖范围,直到覆盖范围覆盖了终点**,这个范围内最小步数一定可以跳到,不用管具体是怎么跳的,不纠结于一步究竟跳一个单位还是两个单位。
139141

140-
141142
## 其他语言版本
142143

144+
### Java
143145

144-
### Java
145146
```Java
146147
// 版本一
147148
class Solution {
@@ -207,7 +208,7 @@ class Solution:
207208
nextDistance = 0
208209
for i in range(len(nums)):
209210
nextDistance = max(i + nums[i], nextDistance)
210-
if i == curDistance:
211+
if i == curDistance:
211212
if curDistance != len(nums) - 1:
212213
ans += 1
213214
curDistance = nextDistance
@@ -230,9 +231,10 @@ class Solution:
230231
step += 1
231232
return step
232233
```
234+
233235
```python
234236
# 动态规划做法
235-
class Solution:
237+
class Solution:
236238
def jump(self, nums: List[int]) -> int:
237239
result = [10**4+1]*len(nums)
238240
result[0]=0
@@ -244,7 +246,6 @@ class Solution:
244246

245247
```
246248

247-
248249
### Go
249250

250251
```go
@@ -331,21 +332,21 @@ var jump = function(nums) {
331332

332333
```typescript
333334
function jump(nums: number[]): number {
334-
const length: number = nums.length;
335-
let curFarthestIndex: number = 0,
336-
nextFarthestIndex: number = 0;
337-
let curIndex: number = 0;
338-
let stepNum: number = 0;
339-
while (curIndex < length - 1) {
340-
nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]);
341-
if (curIndex === curFarthestIndex) {
342-
curFarthestIndex = nextFarthestIndex;
343-
stepNum++;
344-
}
345-
curIndex++;
335+
const length: number = nums.length;
336+
let curFarthestIndex: number = 0,
337+
nextFarthestIndex: number = 0;
338+
let curIndex: number = 0;
339+
let stepNum: number = 0;
340+
while (curIndex < length - 1) {
341+
nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]);
342+
if (curIndex === curFarthestIndex) {
343+
curFarthestIndex = nextFarthestIndex;
344+
stepNum++;
346345
}
347-
return stepNum;
348-
};
346+
curIndex++;
347+
}
348+
return stepNum;
349+
}
349350
```
350351

351352
### Scala
@@ -379,23 +380,25 @@ object Solution {
379380
```Rust
380381
//版本一
381382
impl Solution {
382-
fn max(a: i32, b:i32) -> i32 {
383-
if a > b { a } else { b }
384-
}
385383
pub fn jump(nums: Vec<i32>) -> i32 {
386-
if nums.len() == 0 { return 0; }
387-
let mut cur_distance: i32 = 0;
388-
let mut ans: i32 = 0;
389-
let mut next_distance: i32 = 0;
390-
for i in 0..nums.len() {
391-
next_distance = Self::max(nums[i] + i as i32, next_distance);
392-
if i as i32 == cur_distance {
393-
if cur_distance != (nums.len() - 1) as i32 {
384+
if nums.len() == 1 {
385+
return 0;
386+
}
387+
let mut cur_distance = 0;
388+
let mut ans = 0;
389+
let mut next_distance = 0;
390+
for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) {
391+
next_distance = (n as usize + i).max(next_distance);
392+
if i == cur_distance {
393+
if cur_distance < nums.len() - 1 {
394394
ans += 1;
395395
cur_distance = next_distance;
396-
if next_distance == (nums.len() - 1) as i32 { break; }
396+
if next_distance >= nums.len() - 1 {
397+
break;
398+
};
399+
} else {
400+
break;
397401
}
398-
else { break; }
399402
}
400403
}
401404
ans
@@ -406,16 +409,16 @@ impl Solution {
406409
```Rust
407410
//版本二
408411
impl Solution {
409-
fn max(a: i32, b:i32) -> i32 {
410-
if a > b { a } else { b }
411-
}
412412
pub fn jump(nums: Vec<i32>) -> i32 {
413-
let mut cur_distance: i32 = 0;
414-
let mut ans: i32 = 0;
415-
let mut next_distance: i32 = 0;
416-
for i in 0..nums.len() - 1 {
417-
next_distance = Self::max(nums[i] + i as i32, next_distance);
418-
if i as i32 == cur_distance {
413+
if nums.len() == 1 {
414+
return 0;
415+
}
416+
let mut cur_distance = 0;
417+
let mut ans = 0;
418+
let mut next_distance = 0;
419+
for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) {
420+
next_distance = (n as usize + i).max(next_distance);
421+
if i == cur_distance {
419422
cur_distance = next_distance;
420423
ans += 1;
421424
}
@@ -425,7 +428,6 @@ impl Solution {
425428
}
426429
```
427430

428-
429431
<p align="center">
430432
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
431433
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /