添加(0045.跳跃游戏II.md):补充Java版本2

This commit is contained in:
hutbzc
2022-02-01 23:01:36 +08:00
parent d300529b68
commit 8ac7cdce22

View File

@ -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: