[LeetCode]Jump Game@python
2018-01-23 16:33
495 查看
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A =
A =
思路:遍历数组,维护一个跳跃最大值,如果当前值大于等于最大值,更新,否则原最大值-1
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A =
[2,3,1,1,4], return
true.
A =
[3,2,1,0,4], return
false.
思路:遍历数组,维护一个跳跃最大值,如果当前值大于等于最大值,更新,否则原最大值-1
class Solution(object): def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ max_num = 0 for i in range(len(nums)): if nums[i] >= max_num: max_num = nums[i] else: max_num -= 1 if max_num == 0 and i != len(nums)-1: return False return True
相关文章推荐
- leetcode Jump Game python答案
- LeetCode 55 Jump Game(Python 实现及详解)
- [LeetCode]题解(python):055-Jump Game
- leetcode: Jump Game--python
- [leetcode]55. Jump Game ,C++/PYTHON实现,medium难度
- leetcode(12),Ugly Number(python)
- 【26-30】LeetCode:Python解题
- [LeetCode BY Python]155. Min Stack
- [LeetCode]Remove Duplicates from Sorted Array@python
- python写算法题:leetcode: 2. Add Two Numbers
- leetcode @python 127. Word Ladder
- [LeetCode]题解(python):080-Remove Duplicates from Sorted Array II
- [LeetCode]题解(python):090-Subsets II
- leetcode 计算二叉树中出现最多的元素,Python实现
- [leetcode.com]算法题目 - Jump Game
- leetcode223题 题解 翻译 C语言版 Python版
- 【leetcode】58. Length of Last Word(Python & C++)
- LeetCode 55. Jump Game and 45. Jump Game II 题解
- 【LeetCode with Python】 Search in Rotated Sorted Array
- 【LeetCode】Group Anagrams 解题报告(Python & Java)