LeetCode 54 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 example:

A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

思路一:使用暴力破解,时间复杂度为O(n^2),很简单,代码也很容易懂,代码如下,但是面对一个超级长的数据时,超时。

    public boolean canJump_time_out(int[] A) {
    	if(A.length==1) return true;
    	boolean [] flag =new boolean[A.length];
    	for(int i=0;i<A.length;i++){
    		for(int j=1;j<=A[i]&&(i+j)<A.length;j++){
    			if(flag[i]) flag[i+j]=true;
    		}
    	}
    	return flag[A.length-1];
    }

思路二:稍微使用了一点小技巧,假设A[i]是可以到达的,这是大前提,一定要在这个前提下进行。若A[i]>0,则说明从A[k]到A[A[i]+i]都是可以到达的,下标直接由i变到A[i]+i,若此时i+1>=A.length,则说明最后一个位置是可以到达的,此时返回true;若A[i]为0,则反向遍历,从i到0,若能找到一个下标j,使得j+A[j]>i,则说明A[i]这个位置是可以跳过去的,将j+A[j]赋值给i,若不能找到,则说明i是能够到达的最远位置,而此时j变成了-1,此时返回false;代码如下:

public class Solution {
    public boolean canJump(int[] A) {
    	if(A.length==1) return true;
    	for(int i=0;i<A.length;){
    		if(A[i]>0)  i+=A[i];
    		else {
    			int j=i-1;
    			for(;j>=0;j--){
    				if(j+A[j]>i){
    					i=j+A[j];
    					break;
    				}
    			}
    			if(j<0) return false;
    		}
    		if(i+1>=A.length) return true;
    	}
    	return false;
    }
}
时间: 2024-11-07 20:44:25

LeetCode 54 Jump Game的相关文章

【LeetCode】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 example:A = 

【LeetCode】Jump Game II

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】Jump Game II 解题报告

[题目] 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

[LeetCode]55.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 example:

LeetCode 45 Jump Game II(按照数组进行移动)

题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. 求解出从下标为0开始到下标到数组最后一个所需要的最少跳动次数! 1.当数组为空或者数组长度等于1时,不需要跳动,因此返回0 否则初始化step=1 2.初始化left=0 right = nums[0] 当left<=right时进入循环体中. 3.从第i=0开始,如果当前跳动距离大于数组长度则返回st

【LeetCode】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 example

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][JavaScript]Jump Game

https://leetcode.com/problems/jump-game/ 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

[LeetCode] Frog Jump 青蛙过河

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord