您的位置:首页 > 其它

[LeetCode]Jump Game

2017-02-23 10:59 309 查看
https://leetcode.com/problems/jump-game/?tab=Description

数组表示青蛙下次能跳的距离,问能否跳到结尾

max记录目前能跳的最远位置,如果当前遍历位置已经跳不到,就永远跳不到结尾了

public class Solution {
public boolean canJump(int[] nums) {
int max = 0;
for (int i = 0; i < nums.length; i++) {
if (i > max) {
return false;
}
max = Math.max(i + nums[i], max);
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: