LeetCode 55 Jump Game(Python 实现及详解)
2017-07-27 17:13
567 查看
Given an array of non-negative integers,you are initially positioned at the first index of the array.
Each element in the array represents yourmaximum jump length at that position.
Determine if you are able to reach the lastindex.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
A = [3,2,2,0,4], return true.
题意:
给你一组正整数,数组里面数字表示该位置可以跳多少步。如果从第一个跳到最后那个数,就返回成功否则返回失败。
比如说A =[2,3,1,1,4]
默认位置为A[0],
方案一:A[0]往后最多跳2步--->A[2],A[2]能往后最多跳1步--->A[3],A[3]能往后最多跳1步--->A[4].(到了最后一个,无论该位置是多少都是True)
方案二: A[0]能往后跳1步--->A[1],A[1]跳3步--->A[4]
比如A = [3,2,1,0,4]
从默认位置A[0]能往后跳3步--->A[3],A[3] = 0也就是只能往后0个,不能到最后。经过多次尝试发现,无论如何不能到最后,则返回false
思路:
遍历数组,剩余步数step不为零的前提下,每次向前移动一步,将当前的num[i]和step相比较取较大者,作为剩余步数step。
【python 代码】
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
step = nums[0]
for i in range(1, len(nums)):
if step > 0:
step -= 1#默认向前走一步,即从i走到i+1,剩余步数减1
step = max(step, nums[i])#将此位置步数与step作比较,最大值当做step
else:
return False
return True
if __name__ == '__main__':
S= Solution()
A= [3,2,2,0,4]
S.canJump(A)
Each element in the array represents yourmaximum jump length at that position.
Determine if you are able to reach the lastindex.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
A = [3,2,2,0,4], return true.
题意:
给你一组正整数,数组里面数字表示该位置可以跳多少步。如果从第一个跳到最后那个数,就返回成功否则返回失败。
比如说A =[2,3,1,1,4]
默认位置为A[0],
方案一:A[0]往后最多跳2步--->A[2],A[2]能往后最多跳1步--->A[3],A[3]能往后最多跳1步--->A[4].(到了最后一个,无论该位置是多少都是True)
方案二: A[0]能往后跳1步--->A[1],A[1]跳3步--->A[4]
比如A = [3,2,1,0,4]
从默认位置A[0]能往后跳3步--->A[3],A[3] = 0也就是只能往后0个,不能到最后。经过多次尝试发现,无论如何不能到最后,则返回false
思路:
遍历数组,剩余步数step不为零的前提下,每次向前移动一步,将当前的num[i]和step相比较取较大者,作为剩余步数step。
【python 代码】
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
step = nums[0]
for i in range(1, len(nums)):
if step > 0:
step -= 1#默认向前走一步,即从i走到i+1,剩余步数减1
step = max(step, nums[i])#将此位置步数与step作比较,最大值当做step
else:
return False
return True
if __name__ == '__main__':
S= Solution()
A= [3,2,2,0,4]
S.canJump(A)
相关文章推荐
- LeetCode 70 Climbing Stairs(Python详解及实现)
- LeetCode 60 Permutation Sequence (Python实现及详解)
- LeetCode 77 Combinations (Python详解及实现)
- LeetCode 75 SortColor(Python详解及实现)
- LeetCode 81 Search in Rotated Sorted Array II (Python详解及实现)
- LeetCode 71 Simplify Path(Python详解及实现)
- LeetCode 65 ValidNumer(Python详解及实现)
- LeetCode 86 Partition List (Python详解及实现)
- LeetCode 91 Decode Ways (Python详解及实现)
- LeetCode 79 Word Search (Python详解及实现)
- LeetCode 92 Reverse Linked List II (Python详解及实现)
- [leetcode]55. Jump Game ,C++/PYTHON实现,medium难度
- LeetCode 87 Scramble String (Python详解及实现)
- 25_LeetCode 82&83. Remove Duplicates from Sorted List(II) (Python详解及实现)
- LeetCode 74 Search a 2D Matrix(Python详解及实现)
- LeetCode 89 Gray Code (Python详解及实现)
- LeetCode 66 Plus One (Python详解及实现)
- LeetCode 84 Largest Rectangle in Histogram (Python详解及实现)
- LeetCode 68 Text Justification(Python详解及实现)
- LeetCode 76 Minimum Window Substring(Python详解及实现)