Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

跳跃游戏 #1061

Open
pwstrick opened this issue Jul 31, 2020 · 0 comments
Open

跳跃游戏 #1061

pwstrick opened this issue Jul 31, 2020 · 0 comments
Labels
Leetcode Leetcode的题目

Comments

@pwstrick
Copy link
Owner

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;
};
@pwstrick pwstrick added the Leetcode Leetcode的题目 label Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Leetcode Leetcode的题目
Projects
None yet
Development

No branches or pull requests

1 participant