题目来源


https://leetcode.com/problems/jump-game-ii/

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.


题意分析


Input: a list named nums

Output:minimum steps to the last index

Conditions:最小的步数到达最后


题目思路


网上大牛的代码好厉害= =想了老半天才想懂,主要思想是每一次跳跃时记录一个能到达的最大位置,然后在这个位置前记录所能到达的位置的最大位置(不断更新),当到达之前步数记录最大位置时,step+1,然后赋值新的最大位置,然后继续遍历下去(感觉这样说我都不明白……直接看代码)


AC代码(Python)


 class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
last = 0 #the number steps
step = 0 #current max
curr = 0 for i in range(len(nums)):
if i > last:
step += 1
last = curr
curr = max(curr, i + nums[i]) return step

最新文章

  1. 【C#】类单例 可以解决全局变量的问题
  2. sql 2008 游标
  3. jquery的children方法和css3选择器配合使用
  4. 使用streaming window函数统计用户不同时间段平均消费金额等指标
  5. 1962-Fibonacci
  6. (转载)让ie6也支持max-width,和max-height实现图片等比例缩放
  7. C# 添加、修改和删除PDF书签
  8. MicroPython最全资料集锦丨TPYBoard全系列教程之文档+例程源码
  9. git在windows及linux环境下安装及常用命令
  10. CentOS 7 配置DHCP中继代理服务
  11. 安卓工作室 Android studio 或 Intellij IDEA 美化 修改 汉化 酷炫 装逼 Android studio or Intellij IDEA beautify modify Chinesization cool decoration
  12. POJ.2891.Strange Way to Express Integers(扩展CRT)
  13. 常见C语言编译错误解析【转】
  14. nodejs事件的监听与事件的触发
  15. es6模块 nodejs模块和 typescript模块
  16. webpack2.0+ vue2.0
  17. 【转】Understanding the Angular Boot Process
  18. Ubuntu 13.04 安装 Oracle11gR2
  19. yum whatprovides host 根据命令查找包
  20. 自动将 NuGet 包的引用方式从 packages.config 升级为 PackageReference

热门文章

  1. BZOJ3641 : 货车运输
  2. 生成CSV文件后再将CSV文件导入到mysql
  3. Graph database_neo4j 底层存储结构分析(4)
  4. Hashtable和Dictionary<T,K>的使用
  5. shell总结(0基础入门)
  6. mof提权带回显带清楚命令版本.php
  7. FastDFS 安装
  8. explicit关键字
  9. RT-Thread的线程间同步
  10. Bootstrap页面布局15 - BS带下拉菜单的按钮