0
点赞
收藏
分享

微信扫一扫

Leetcode55. 跳跃游戏


题目传送地址: ​​https://leetcode.cn/problems/jump-game/​​

Leetcode55. 跳跃游戏_游戏


代码如下:

public static boolean canJump(int[] nums) {
//处理边界条件
if (nums.length == 1) {
return true;
}
for (int i = 0; i < nums.length - 1; i++) {
int j = i + 1;
int nextStep = i; //下一步应该跳的位置
int farthest = 0; //最远处
while (j < nums.length && j <= i + nums[i]) {
if (j + nums[j] > farthest) {
farthest = j + nums[j];
nextStep = j;
}
j++;
}
if (farthest >= nums.length - 1) {
return true;
}
if (nums[nextStep] == 0 && nextStep < nums.length - 1) {
return false;
}
i = nextStep - 1;
}
return false;
}


举报

相关推荐

0 条评论