Merge pull request #124 from LiangDazhu/patch-4

添加 0045.跳跃游戏II python版本
This commit is contained in:
Carl Sun 2021-05-15 17:16:57 +08:00 committed by GitHub
commit 8ba14df96a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 1 deletions

View File

@ -175,7 +175,22 @@ class Solution {
```
Python
```python
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) == 1: return 0
ans = 0
curDistance = 0
nextDistance = 0
for i in range(len(nums)):
nextDistance = max(i + nums[i], nextDistance)
if i == curDistance:
if curDistance != len(nums) - 1:
ans += 1
curDistance = nextDistance
if nextDistance >= len(nums) - 1: break
return ans
```
Go