1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 116 · 跳跃游戏 Jump Game

116 · 跳跃游戏 Jump Game

时间:2021-09-23 13:54:31

相关推荐

116 · 跳跃游戏 Jump Game

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 hereint 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 ;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。