【Leetcode】经典的Jump Game in 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.

这道题的巧妙之处在于,每一个值可以跳“值”大小步数。所以我们的思路如下:

1.记录到现在为止能跳到最远的距离,记为max

2.每一个点能走的步数,记为step,且每往前面走一步,step--

3.判断到这个点后往后跳的步数是不是大于max,如果是就更新max,不是就继续往前走

这样的话我们可以看到:如果前面这个点是零,且这个时候step步数不够你迈过去,那么就会自动跳出返回false。但是如果可以一直这么走走到终点,那么返回true

package testAndfun;

public class jumpGame {
	public static void main(String args[]){
		int[] A = {3,0,0,0};
		jumpGame jp = new jumpGame();
		if(jp.canJump(A))	System.out.println("We win");
		else System.out.println("we lose");
	}

    public boolean canJump(int[] A) {
    	int max = 0;
    	int step = 1;
    	if(A.length<=1)	return true;
    	if(A[0]==0&&A.length>1) return false;
    	for(int i=0;i<A.length;i++){

    	    step--;
    		if(i+A[i]>max){
    			max = i+A[i];
    			step = A[i];
    		}
    		if(step==0 && i<A.length-1)	return false;

    	}
    	return true;
}
    }
时间: 2024-08-04 09:29:51

【Leetcode】经典的Jump Game in JAVA的相关文章

经典面试题回答——学习Java基础的目的

本系列知识解释:相信每一个学习Java的人都是从JavaSE开始的,也就是Java基础开始.但是却并不清楚学习Java基础到底有什么用? 首先我来回答这个问题,学习Java基础是有两个目的: 一.掌握基本的Java语言底层编码方法,同时掌握Java这门语言的编程思想,为了后期学习JavaEE打下基础,要不然在学习JavaEE的时候,连封装,继承,多态都不懂,那就实在是没法说了(相信在传智播客学习过Java基础的同学,这些都不是问题.) 二.在学习完JavaSE和JavaEE后招工作的时候能够答上

leetcode 119 Pascal&#39;s Triangle II ----- java

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

算法基础——经典八大排序算法的Java及Python实现

概述 八大排序算法不用多说了,程序员算法基础必须要掌握的,现在总结一下加深记忆.下图是这八大排序算法的分类.名称.时间空间复杂度,以及稳定性. 代码 以下是经典八大排序算法的Java及Python代码,都是基于经典算法书籍<算法导论>里的伪代码实现的,我在关键语句部分附上了注释. 按照上图中的顺序分别介绍八大排序算法的实现(升序),前面是Java,后面是Python.Java的排序函数写在了一个类里,Python的排序函数则直接写出来了. 直接插入排序 public class InsertS

leetcode 45 Jump Game II ---- 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. Your goal is to reach the last index in the minimum number of jumps

LeetCode 55 _ Jump Game 跳跃游戏

Description: 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. Exa

【Leetcode】Evaluate Reverse Polish Notation JAVA

   一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra spa

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 006.ZigZag_Conversion (Easy) 链接: 题目:https://oj.leetcode.com/problems/zigzag-conversion/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个字符串按横写的折线排列. 分析: 直

[leetcode解题记录]Jump Game和Jump Game II

Jump Game 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 ex