LeetCode0055.跳跃游戏

题目要求

算法分析

贪心算法

从起点开始遍历直到所能到达的最远位置,

过程中更新所能到达的最远索引,如果能到达的最远索引大于终点则返回真,否则为假

代码展示(C#)

public class Solution {
    public bool CanJump(int[] nums) {
        int maxIndex = nums[0];
        int tempLength;
        for(int i = 0; i <= maxIndex; i++){
            tempLength = nums[i];
            if( i + tempLength > maxIndex){
                maxIndex = i + tempLength;
            }
            if(maxIndex >= nums.Length - 1){
                return true;
            }
        }
        return false;
    }
}

提交结果

原文地址:https://www.cnblogs.com/KingR/p/13092081.html