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

      

  • 相关阅读:
    《自拍教程17》Python调用命令
    c和c++学哪个?
    PHP:变量之效果域、静态变量,常量等基础知识
    Java中NIO及基础实现
    零代码=零门槛?
    程序员真的都比较宅吗?
    DataGridView怎样完成添加、删除、上移、下移一行
    C# 控件 RichTextBox 显示行号,而且与Panel彼此联动
    C语言代码中的空白符表示什么
    php 中的4种标记风格介绍
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11307598.html
Copyright © 2011-2022 走看看