刷题记录(Jump Game II)

Leecode 45:Jump Game II (JS答题)

刷题记录(Jump Game II)
解法一:

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;
};
上一篇:P6982 [NEERC2015]Jump


下一篇:贪心 LeetCode 55. Jump Game (medium)