<html>

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.

从后往前找是0的元素(不包含最后一个元素)。然后往前搜索能否找到能跨越这个零点的元素。假设找不到则返回false。假设能找到则跳到该找到元素的前一个元素继续操作,反复上述步骤。

 

public class Solution {
    public boolean canJump(int[] A) {
        for(int i=A.length-2;i>=0;i--){
        	if(A[i]==0){
        		boolean oneStep = false;
        		for(int j = i-1;j>=0;j--){
        			if(A[j]+j>i){ 
        				oneStep = true;
        				i = j;
        				break;
        			}
        		}
        		if(!oneStep) return false;
        	}
        }
        return true;
    }
}





版权声明:本文为博主原创文章,未经博主同意不得转载。 举报
  • 本文已收录于下面专栏:

相关文章推荐

LeetCode 55/45 Jump Game I/II-----Greedy**

一:Jump Game 题目: Given an array of non-negative integers, you are initially positioned at the first ...

LeetCode Jump Game II 前跳游戏II

Jump Game II </s
  • jgsj
  • jgsj
  • 2013-12-07 09:02
  • 84

LeetCode045 Jump Game II

具体见:leetcode.com/problems/jump-game-ii Java Solution: github package leetcode; /* 大致意思是: ...

Jump Game II -- LeetCode

原题链接:<a target="_blank" href="http://oj.leetcode.com/problems/jump-game-ii/" style="text-decoration: none; color: rgb(33,135,187); font-family: Aria
  • bcyy
  • bcyy
  • 2014-03-20 23:25
  • 30

LeetCode --- 45. Jump Game II

题目链接:Jump Game II Given an array of non-negative integers, you are initially positioned at the firs...

[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 la

leetcode -- Jump Game II -- 贪心,要看

https://leetcode.com/problems/jump-game-ii/dp 会TLE思路1 会TLE參考http://chaoren.is-programmer.com/posts/4...

[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 la

Leetcode Algorithm 045. Jump Game II

Leetcode Algorithm 045. Jump Game II 给定一个非负整数的数组,数组中的元素表示从当前位置能够跳过的最长步数;从数组的第一个位置。能够经过中途的某些点跳到最后一个位置...

[leetcode]Jump Game

新博文地址:<a id="cb_post_title_url" style="font-size: 1.5em; line-height: 1.5em; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: #336699; text-decoration: underline;" href="http://www.cnblogs.com/huntfor/p/3898452.h
  • 微博
    微信
    QQ
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多仅仅同意输入30个字)

posted @ 2017-08-17 20:40  jhcelue  阅读(167)  评论(0编辑  收藏  举报