zoukankan      html  css  js  c++  java
  • Jump Game leetcode java

    题目

    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.

    题解

     参考了http://fisherlei.blogspot.com/2012/12/leetcode-jump-game.html的代码

     很巧妙的解法,代码很短,看着一眼就能看懂,但自己想不见得想的出来,好好品味一下

    代码如下:

     1     public boolean canJump(int[] A) {  
     2             int maxCover = 0;  
     3             for(int start =0; start<= maxCover && start<A.length; start++)  
     4             {  
     5                  if(A[start]+start > maxCover)  
     6                       maxCover = A[start]+start;  
     7                  if(maxCover >= A.length-1) 
     8                     return true;  
     9             }  
    10             return false;  
    11        } 

  • 相关阅读:
    dos常用命令
    组合封装知识点
    继承与派生知识点
    继承与派生
    面向对象知识点
    面向对象
    Day 84 DRF的分页和过滤
    Day80 使用第三方(腾讯云)短信验证码接口
    Day 79 xadmin后台管理/Git仓库
    Day 77 三大认证组件
  • 原文地址:https://www.cnblogs.com/springfor/p/3872320.html
Copyright © 2011-2022 走看看