From 8ac7cdce22a6f350ec57627e474bd482f5d041b3 Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:01:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII.md=EF=BC=89=EF=BC=9A=E8=A1=A5?= =?UTF-8?q?=E5=85=85Java=E7=89=88=E6=9C=AC2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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: