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

      

  • 相关阅读:
    数组以字符串记录(字符串转数组)
    linux下OpenSSL的RSA密钥生成
    php rsa加密解密实例 及签名验证-自己实践
    php rsa加密解密实例
    PHP的openssl加密扩展使用小结
    支付宝开放平台 配置RSA(SHA1)密钥 OpenSSL配置公钥私钥对
    HTTP缓存控制
    java去任意范围的随机数
    (转)Eclipse4.2 Tomcat启动报错 A child container failed during start
    模态框事件介绍
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11307598.html
Copyright © 2011-2022 走看看