diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index a39c064a..7a3f048c 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -142,6 +142,7 @@ public: ### Java ```Java +// 版本一 class Solution { public int jump(int[] nums) { if (nums == null || nums.length == 0 || nums.length == 1) { @@ -172,7 +173,30 @@ class Solution { } ``` +```java +// 版本二 +class Solution { + public int jump(int[] nums) { + int result = 0; + // 当前覆盖的最远距离下标 + int end = 0; + // 下一步覆盖的最远距离下标 + int temp = 0; + for (int i = 0; i <= end && end < nums.length - 1; ++i) { + temp = Math.max(temp, i + nums[i]); + // 可达位置的改变次数就是跳跃次数 + if (i == end) { + end = temp; + result++; + } + } + return result; + } +} +``` + ### Python + ```python class Solution: def jump(self, nums: List[int]) -> int: