116 · 跳跃游戏 Jump Game

这是一个关于算法的问题,名为跳跃游戏。给定一个非负整数数组,初始位置为数组的第一个元素。每个元素表示当前位置的最大跳跃长度。任务是判断是否能到达数组的最后一个元素。提供的Java代码实现中,通过维护当前最大可达位置`maxlength`来判断是否能成功跳跃到末尾。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

116 · 跳跃游戏 Jump Game

描述
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.
样例
Example 1:

Input:

A = [2,3,1,1,4]
Output:

true
Explanation:

0 -> 1 -> 4 (the number here is subscript) is a reasonable scheme.

public class Solution {
    /**
     * @param A: A list of integers
     * @return: A boolean
     */
    public boolean canJump(int[] A) {
        // write your code here
        
        
        int maxlength = 0 ;
        
        for(int i = 0 ; i < A.length ; i++){
           if(i > maxlength){
                return false ;
            }
            
            maxlength = Math.max(maxlength , A[i] + i);
            if(maxlength >= A.length-1){
                return true ;
            }
        }
        
        return false ;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值