Skip to content

Commit 4458bb6

Browse files
committed
二刷55
1 parent bdae3f9 commit 4458bb6

File tree

4 files changed

+82
-20
lines changed

4 files changed

+82
-20
lines changed

docs/0055-jump-game.adoc

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,62 @@
11
[#0055-jump-game]
2-
= 55. Jump Game
2+
= 55. 跳跃游戏
33

4-
{leetcode}/problems/jump-game/[LeetCode - Jump Game^]
4+
https://leetcode.cn/problems/jump-game/[LeetCode - 55. 跳跃游戏 ^]
55

6-
Given an array of non-negative integers, you are initially positioned at the first index of the array.
6+
给你一个非负整数数组 `nums` ,你最初位于数组的 *第一个下标*。数组中的每个元素代表你在该位置可以跳跃的最大长度。
77

8-
Each element in the array represents your maximum jump length at that position.
8+
判断你是否能够到达最后一个下标,如果可以,返回 `true` ;否则,返回 `false`
99

10-
Determine if you are able to reach the last index.
10+
*示例 1:*
1111

12-
*Example 1:*
12+
....
13+
输入:nums = [2,3,1,1,4]
14+
输出:true
15+
解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
16+
....
1317

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* [2,3,1,1,4]
17-
*Output:* true
18-
*Explanation:* Jump 1 step from index 0 to 1, then 3 steps to the last index.
19-
----
18+
*示例 2:*
2019

21-
*Example 2:*
20+
....
21+
输入:nums = [3,2,1,0,4]
22+
输出:false
23+
解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
24+
....
2225

23-
[subs="verbatim,quotes,macros"]
24-
----
25-
*Input:* [3,2,1,0,4]
26-
*Output:* false
27-
*Explanation:* You will always arrive at index 3 no matter what. Its maximum
28-
jump length is 0, which makes it impossible to reach the last index.
29-
----
26+
*提示:*
27+
28+
* `1 \<= nums.length \<= 10^4^`
29+
* `0 \<= nums[i] \<= 10^5^`
30+
31+
32+
== 思路分析
3033

34+
遍历数组,记录当前位置能到达的最远距离,如果最远距离超过了数组长度,则可以。如果中途最远距离小于等于当前坐标,则无法向前,如果还没到达数组尾部,则是不能到达。
3135

3236
[[src-0055]]
37+
[tabs]
38+
====
39+
一刷::
40+
+
41+
--
3342
[{java_src_attr}]
3443
----
3544
include::{sourcedir}/_0055_JumpGame.java[tag=answer]
3645
----
46+
--
47+
48+
二刷::
49+
+
50+
--
51+
[{java_src_attr}]
52+
----
53+
include::{sourcedir}/_0055_JumpGame_2.java[tag=answer]
54+
----
55+
--
56+
====
57+
58+
59+
== 参考资料
3760

61+
. https://leetcode.cn/problems/jump-game/solutions/203549/tiao-yue-you-xi-by-leetcode-solution/[55. 跳跃游戏 - 官方题解^]
62+
. https://leetcode.cn/problems/jump-game/solutions/46397/pythonji-bai-97kan-bu-dong-ni-chui-wo-by-mo-lan-4/[55. 跳跃游戏 - Python 击败 97%,看不懂你锤我^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ endif::[]
728728
|{doc_base_url}/0080-remove-duplicates-from-sorted-array-ii.adoc[题解]
729729
|✅ 双指针,同时需要记录每个元素的次数,相同元素只复制两个。
730730

731+
|{counter:codes2503}
732+
|{leetcode_base_url}/jump-game/[55. 跳跃游戏^]
733+
|{doc_base_url}/0055-jump-game.adoc[题解]
734+
|✅ 贪心算法。没想到这就是贪心?
735+
731736
|===
732737

733738
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0055_JumpGame.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class _0055_JumpGame {
3939
* Runtime: 1 ms, faster than 99.35% of Java online submissions for Jump Game.
4040
*
4141
* Memory Usage: 37.7 MB, less than 100.00% of Java online submissions for Jump Game.
42+
*
43+
* @author D瓜哥 · https://www.diguage.com
44+
* @since 2019-10-23 12:25
4245
*/
4346
public boolean canJump(int[] nums) {
4447
if (Objects.isNull(nums) || nums.length <= 1) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0055_JumpGame_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-19 14:50:01
9+
*/
10+
public boolean canJump(int[] nums) {
11+
int max = nums[0];
12+
for (int i = 0; i < nums.length; i++) {
13+
max = Math.max(max, i + nums[i]);
14+
if (max >= nums.length - 1) {
15+
return true;
16+
}
17+
if (max < i) {
18+
return false;
19+
}
20+
}
21+
return false;
22+
}
23+
24+
// end::answer[]
25+
public static void main(String[] args) {
26+
new _0055_JumpGame_2()
27+
.canJump(new int[]{3, 2, 1, 0, 4});
28+
}
29+
}

0 commit comments

Comments
 (0)