Skip to content

Commit 8ac7cdc

Browse files
committed
添加(0045.跳跃游戏II.md):补充Java版本2
1 parent d300529 commit 8ac7cdc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

problems/0045.跳跃游戏II.md

Lines changed: 24 additions & 0 deletions
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:

0 commit comments

Comments
 (0)