【Leetcode】Jump Game
2016-05-23 18:04
316 查看
题目链接:https://leetcode.com/problems/jump-game/
题目:
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 = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
思路:
当可以往前走的步数为0的时候,返回false
否则尽力往前走,并更新可以往前走的步数。
算法:
题目:
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 = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
思路:
当可以往前走的步数为0的时候,返回false
否则尽力往前走,并更新可以往前走的步数。
算法:
public boolean canJump(int[] nums) { if (nums.length == 0 || nums.length == 1) return true; int maxStep = nums[0]; for (int i = 1; i < nums.length; i++) { if (maxStep == 0) return false; maxStep--; if (maxStep < nums[i]) { maxStep = nums[i]; } if (maxStep + i >= nums.length - 1) { return true; } } return false; }
相关文章推荐
- 【Leetcode】Basic Calculator
- 104. Maximum Depth of Binary Tree
- hdu1584 蜘蛛牌 --DFS & 区间DP
- tail命令
- 删除用户自定义配置
- Linux下二维码生成工具:QRencode
- JDK与JRE的区别
- 【Leetcode】Maximum Product of Word Lengths
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- 视频格式转换
- 文件夹目录下文件太多,list直接卡住的解决方式
- zookeeper 的安装和伪集群配置
- 【Leetcode】Basic Calculator II
- Spark中组件Mllib的学习14之从文本中读取带标签的数据,生成带label的向量
- iOS前端与后台交互技术实现及技术细节
- 基于WADE框架开发流程
- 如何解决PowerPoint课件中的菜单问题
- 通过MFCMAPI工具删除用户的邮件
- 关系与非关系型数据库
- unity 打包IPA,减少文件大小的方法