You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var canJump = function(nums) {
let jumpStep = 1;
let i = 0;
for(; i < nums.length; i++) {
let next = 0;
let max = 0;
for (let j = 0; j < jumpStep; j++) {
if (i + j + nums[i + j] > max) {
next = i + j;
max = i + j + nums[i + j];
}
}
i = next;
console.log(i);
jumpStep = nums[i];
if (jumpStep === 0) break;
}
if (i < nums.length - 1) {
console.log(false)
return false;
}
console.log(true)
return true;
};
The text was updated successfully, but these errors were encountered:
leetcode 55. 跳跃游戏
题目描述
解题思路:
数组的每一个位置记录了当前路程和它能跳到的最远路程,我们只要找到这个位置,并根据这个位置不断寻找下一个位置
若能走完数组,true
根据以上算法走不完,false
符合动态规划,算法复杂度为O(n)
代码如下
The text was updated successfully, but these errors were encountered: