项目作者: eMahtab

项目描述 :
Jump Game
高级语言:
项目地址: git://github.com/eMahtab/jump-game.git
创建时间: 2020-04-27T01:00:54Z
项目社区:https://github.com/eMahtab/jump-game

开源协议:

下载


Jump Game

https://leetcode.com/problems/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.

  1. Example 1:
  2. Input: [2,3,1,1,4]
  3. Output: true
  4. Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
  5. Example 2:
  6. Input: [3,2,1,0,4]
  7. Output: false
  8. Explanation: You will always arrive at index 3 no matter what.
  9. Its maximum jump length is 0, which makes it impossible to reach the last index.

Implementation :

  1. class Solution {
  2. public boolean canJump(int[] nums) {
  3. int n = nums.length;
  4. int lastGoodIndex = n-1;
  5. for(int i = n-2; i >= 0; i--) {
  6. if(i+nums[i] >= lastGoodIndex) {
  7. lastGoodIndex = i;
  8. }
  9. }
  10. return lastGoodIndex == 0;
  11. }
  12. }

References :

https://www.youtube.com/watch?v=Zb4eRjuPHbM