[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 = [2,3,1,1,4], return true.

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

// 一维的动态规划问题

 1 public class Solution {
 2     public boolean canJump(int[] A) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         int n = A.length;
 6         if(n<=1)
 7             return true;
 8         boolean [] ret = new boolean[n];
 9         ret[n-1] = true;
10         for(int i=n-2; i>=0; i--)
11         {
12             int max_len = A[i];
13             for(int j=i+1; j<n && j<=i+max_len; j++)
14             {
15                 ret[j] = true;
16                 break;
17             }
18         }
19         return ret[0];
20     }
21 }

 

 

posted on 2013-06-11 15:45  brave_bo  阅读(343)  评论(0)    收藏  举报

导航