Jump Game
来源:互联网 发布:程序编程和软件开发 编辑:程序博客网 时间:2024/05/16 12:41
Problem:
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
.
F(i)表示在i能跳的最远距离,
F(0)=A[0];
对于i>0,F(i)=max(F(i-1)-1,A[i])。
如果A的长度小于1,返回真。
如果A的长度大于1,除末节点外,其他节点都必须满足F(i)>0,否则返回假。
Solution:
public class Solution {
public boolean canJump(int[] A) {
if(A==null||A.length<=1)
return true;
if(A[0]==0)
return false;
for(int i=1;i<A.length-1;i++)
{
A[i] = Math.max(A[i-1]-1, A[i]);
if(A[i]<=0)
return false;
}
return true;
}
}
public boolean canJump(int[] A) {
if(A==null||A.length<=1)
return true;
if(A[0]==0)
return false;
for(int i=1;i<A.length-1;i++)
{
A[i] = Math.max(A[i-1]-1, A[i]);
if(A[i]<=0)
return false;
}
return true;
}
}
0 0
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- JavaFX 8 教程
- 15道使用频率极高的基础算法题
- 手机PCB设计时RF布局技巧
- java里抽象类和接口的区别
- 【ACdream】Andrew Stankevich's Contest (4)
- Jump Game
- 第十七章 17.3.3节练习
- C++实现hash_set
- Java-Iterator的用法
- zoj3715Kindergarten Election(枚举+贪心)
- 本地jar包依赖和运行包
- android_Intent对象初步(Activity传值)
- Thrift Java使用实例(修改版)
- Codeforces Round #271 (Div. 2) C. Captain Marmot (暴力枚举+正方形判定)