Leecode 45:Jump Game II (JS答题)
解法一:
function jump(nums: number[]): number {
var end:number = 0;
var maxPosition:number = 0;
var resCount:number = 0;
for(var i = 0; i < nums.length-1; i++){
maxPosition = Math.max(maxPosition, i + nums[i]);
if(i === end){
end = maxPosition;
resCount++;
}
}
return resCount;
};
解法二:
function jump2(nums: number[]): number {
var position:number = nums.length - 1;
var resCount:number = 0;
while(position > 0){
for(var i = 0; i < position; i++){
if((nums[i] + i) >= position){
position = i;
resCount++;
break;
}
}
}
return resCount;
};