zoukankan      html  css  js  c++  java
  • jump-game

    /**
    *
    * @author gentleKay
    * 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], returntrue.
    * A =[3,2,1,0,4], returnfalse.
    *
    * 给定一个非负整数数组,您最初定位在数组的第一个索引处。
    * 数组中的每个元素表示该位置的最大跳跃长度。
    * 确定是否能够达到最后一个索引。
    * 例如:
    * A=[2,3,1,1,4],返回真。
    * A=[3,2,1,0,4],返回假。
    */

    /**
     * 
     * @author gentleKay
     * 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], returntrue.
     * 		A =[3,2,1,0,4], returnfalse.
     * 
     * 给定一个非负整数数组,您最初定位在数组的第一个索引处。
     * 数组中的每个元素表示该位置的最大跳跃长度。
     * 确定是否能够达到最后一个索引。
     * 例如:
     * 		A=[2,3,1,1,4],返回真。
     * 		A=[3,2,1,0,4],返回假。
     */
    
    public class Main34 {
    	public static void main(String[] args) {
    		int[] A = {2,0,0};
    		System.out.println(Main34.canJump(A));
    	}
    	
    	public static boolean canJump(int[] A) {
    		if (A.length <= 0) {
    			return false;
    		}
    		
    		if (A.length == 1) {
    			return true;
    		}
    		
    		int index = 0;
    		for (int i=0;i<A.length; ) {
    			index = A[i];
    			i = i + index;
    			if (i >= A.length-1) {
    				return true;
    			}
    			if (A[i] == 0) {
    				return false;
    			}	
    		}
            return true;
        }
    }
    

      

  • 相关阅读:
    多网卡ip选择
    微软编程一小时--微软2014实习生招募编程模拟测试感想
    .NET和JAVA的比较- 体系结构
    CentOS下JAVA WEB 环境搭建
    MySQL 8.0.23 安装配置向导
    uniapp map层级太高,样式支持度不高 使用nvue解决
    flex 伸缩盒子
    setInterval在浏览器切换时加速的问题
    软件包查找下载https://pkgs.org/
    斐波那契数列
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11307598.html
Copyright © 2011-2022 走看看