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

Jump Game | Java最短代码实现

2016-03-14 19:54 405 查看
原题链接:55. Jump Game
【思路】

本题扩展:Jump Game 。对任意一点i,maxIndex记录当前能跳到的最大索引,当这个值大于等于nums.length - 1,则返回true,否则返回false。当然nums.length = 1时也返回true:

public boolean canJump(int[] nums) {
int maxIndex = nums[0];
for (int i = 1; i <= maxIndex; i++) {
if (maxIndex >= nums.length - 1) return true;
maxIndex = Math.max(maxIndex, nums[i] + i);
}
return nums.length == 1 ? true : false;
}


72 / 72 test
cases passed. Runtime: 3
ms Your runtime beats 27.06% of javasubmissions.

欢迎优化!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: