Skip to content

Commit

Permalink
添加(0045.跳跃游戏II.md):补充Java版本2
Browse files Browse the repository at this point in the history
  • Loading branch information
hutbzc committed Feb 1, 2022
1 parent d300529 commit 8ac7cdc
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions problems/0045.跳跃游戏II.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public:

### Java
```Java
// 版本一
class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length == 0 || nums.length == 1) {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8ac7cdc

Please sign in to comment.