您的位置:首页 > 编程语言 > PHP开发

FTPrep: 55, Jump Game

2017-09-04 01:18 204 查看
这题和max subarray 有点类似,都是在遍历经过每个点时进行变量的更新,max subarray:
点击打开链接, there are 2 variables there need to be updated, first local, then global, and !! Global is dependent on local !!!靠,这一点我在这里总结出来了!还有一点的话,在max subarray 里要注意的就是难得糊涂,不要就纠结当前点的正负号关系,用这个来做优化,因为这个是起不到什么优化作用的。

这题的话,只有一个变量就是最大距离,没经过一个点都需要更新,然后在下一点的时候进行判断,知道最后没有退出的话就返回true

代码很简单:

class Solution {
public boolean canJump(int[] nums) {
int len=nums.length;
if(len==0) return false;
int maxJump= 0 + nums[0];
for(int i=0; i<len; i++){
if(i>maxJump) return false;
maxJump=Math.max(maxJump, i+nums[i]);
//if(maxJump>=len) return true; adding this operation here actually decrease the efficiency, maybe because in most test case, the ending are pretty late, so not many test case ends early
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: