/** * @param {number[]}nums * @return {boolean} */ var canJump = function (nums) { nums = nums.map((item, index) => [index, item + index]); for (let i = 0; i < nums.length; i++) { if (i === nums.length - 1) returntrue; let arrMin = nums[i][0]; let arrMax = nums[i][1]; while (i < nums.length - 1 && i <= arrMax && nums[i + 1][1] <= arrMax) i++; if (i === nums.length - 1) returntrue; elseif (nums[i + 1][0] > arrMax) returnfalse; } returntrue; };
贪心
1 2 3 4 5 6 7 8 9 10 11 12
/** * @param {number[]}nums * @return {boolean} */ var canJump = function (nums) { let k = 0; for (let i = 0; i < nums.length; i++) { if (i > k) returnfalse; k = Math.max(k, i + nums[i]); } returntrue; };