LeetCode刷题笔录Jump Game
来源:互联网 发布:sentinel1数据下载 编辑:程序博客网 时间:2024/06/07 13:14
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
.
用动态规划来做,jump[i]表示在i位置剩余的最大步数(注意是剩余最大步数,不包括A[i])。
jump[i] = max{jump[i-1], A[i-1]} - 1.
遇到jump[i]<0就返回false.
另外这里只用到jump[i-1],因此不需要一个额外数组来存储jump,只要一个变量就行了。
public class Solution { public boolean canJump(int[] A) { int maxRemain = 0; for(int i = 1; i < A.length; i++){ maxRemain = Math.max(maxRemain, A[i - 1]) - 1; if(maxRemain < 0) return false; } return true; }}
0 0
- LeetCode刷题笔录 Jump Game II
- LeetCode刷题笔录Jump Game
- LeetCode刷题笔录dungeon game
- [leetcode刷题系列]Jump Game
- [leetcode刷题系列]Jump Game II
- LeetCode Jump Game && Jump GameII
- leetcode 刷题之路 54 Jump Game
- leetcode刷题之旅——55. Jump Game
- [LeetCode]Jump Game II、Jump Game
- 【LeetCode】Jump Game & Jump Game II
- leetcode Jump Game & Jump Game II
- leetcode之Jump Game && Jump Game II
- [leetcode]Jump Game &&Jump Game II
- 【LeetCode】Jump Game && Jump Game II
- [Leetcode]Jump Game && Jump Game II
- Leetcode之Jump Game & Jump Game II
- Jump Game 和Jump Game II---LeetCode
- LeetCode-Jump Game & Jump Game II
- 删除所有JTable rows
- iOS Runtime Programing
- developer.biao.daily.20140731
- LeetCode: Evaluate Reverse Polish Notation
- Hibernate 可能遇见的一个错误
- LeetCode刷题笔录Jump Game
- u-boot-2009.08在mini2440上的移植 增加SD卡功能
- N-Queens II
- Python 正则表达式验证zip code (U.S.邮政编码)
- 单向链表(三) 在指定位置插入新的节点
- 转:ffmpeg解码流程
- 老BOJ 03 Guess
- oracle 11g警告日志和监听日志删除方法示例
- 老BOJ 05 Permutation