mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加(0045.跳跃游戏II.md):补充Java版本2
This commit is contained in:
@ -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:
|
||||
|
Reference in New Issue
Block a user