Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
var canJump = function(nums) {
//記錄遍歷到當(dāng)前節(jié)點(diǎn)時(shí),其左邊節(jié)點(diǎn)最遠(yuǎn)可到達(dá)的位置
var rightMost = 1;
for (var i = 0, len = nums.length; i < len; i++) {
//如果這個(gè)節(jié)點(diǎn)在位置之外趋厉,那這個(gè)節(jié)點(diǎn)就訪問(wèn)不到了
if (rightMost < i + 1) break;
//更新最遠(yuǎn)距離
rightMost = Math.max(rightMost, i + 1 + nums[i]);
}
//最遠(yuǎn)距離覆蓋到最后一個(gè)節(jié)點(diǎn),就能出去
return rightMost >= len;
};