Open pwstrick opened 4 years ago
55. 跳跃游戏
/** * @param {number[]} nums * @return {boolean} */ var canJump = function(nums) { if(nums[0] === 0 && nums.length > 1) { return false; } if(nums.length == 1) { return true; } const len = nums.length - 1, distances = []; //计算每个位置所能到达的最远距离 let i=0, j, max = Number.MIN_VALUE; while(i < len) { distances.push(nums[i] + i); let interval = i + nums[i]; for(j=i+1; j<interval && j <len; j++) { //计算范围内的最远距离 distances.push(nums[j] + j); } i = j; //判断当前最远距离是否可以继续循环 max = Math.max.apply(this, distances); if(max < j) break; } return max >= len; };
55. 跳跃游戏