Jump Game II

xiaoxiao2021-02-28  93

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.

For example: A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

代码如下:

public class JumpGame { public boolean jumpGame(int[] nums){ int i=0; for(int reach=0;i<=nums.length&&i<=reach;i++){ reach=Math.max(reach, nums[i]+i); } return i==nums.length; } }

转载请注明原文地址: https://www.6miu.com/read-60589.html

最新回复(0)