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

      

  • 相关阅读:
    POJ 2996 Help Me with the Game (模拟)
    PCL系列——怎样逐渐地配准一对点云
    sublime text3同时编辑多行
    博客搬家
    将博客搬至CSDN
    centos7用xshell可以连接, xftp连接失败!(墙裂推荐)
    重启ssh服务出现Redirecting to /bin/systemctl restart sshd.service
    重装wordpress
    ubuntu 16.04 启用root用户方法
    Ubuntu创建新用户并增加管理员权限(授权有问题)
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11307598.html
Copyright © 2020-2023  润新知