45. Jump Game II leetcode python 2016 new season
2016-01-25 13:14
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 your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =
The minimum number of jumps to reach the last index is
(Jump
to the last index.)
Note:
You can assume that you can always reach the last index.
Show Tags
Have you met this question in a real interview?
Yes
No
Discuss
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = 0
last = 0
reach = 0
for i in range(len(nums)):
if i > last:
last = reach
count += 1
reach = max(reach, i + nums[i])
return count
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =
[2,3,1,1,4]
The minimum number of jumps to reach the last index is
2.
(Jump
1step from index 0 to 1, then
3steps
to the last index.)
Note:
You can assume that you can always reach the last index.
Show Tags
Have you met this question in a real interview?
Yes
No
Discuss
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = 0
last = 0
reach = 0
for i in range(len(nums)):
if i > last:
last = reach
count += 1
reach = max(reach, i + nums[i])
return count
相关文章推荐
- Python 基础【二】 上
- 《Python项目开发实战》--熟悉Python基础后我能做什么?
- python分布式进程
- Python -- collection 系列
- Python 字符编码学习-ascii编码,Unicode和UTF-8之间的关系
- Python开发基础-day2
- Python 基础教程
- Python 爬虫入门(二)—— IP代理使用
- 为什么python运行的慢
- Python IDE(集成开发环境)汇总
- python set集合
- Python开发如何在ubuntu 15.10 上配置vim
- python之路(十)-正则表达式
- python 使用相对路径import 模块的方法
- python之路(九)-函数装饰器
- python之路(八)-迭代器&生成器
- python之路(七)-递归算法
- learn python the hard way(笨办法学python) 练习40 类的用法
- 在python3.3后urllib2已经不能再用,只能用urllib.request来代替
- python 常用模块