代码改变世界

leetcode - Jump Game

2013-04-21 10:43  张汉生  阅读(119)  评论(0编辑  收藏  举报

题目描述:点击此处

 1 class Solution {
 2 public:
 3   bool canJump(int A[], int n) {
 4     // Start typing your C/C++ solution below
 5     // DO NOT write int main() function
 6     if (n<=0)
 7       return true;
 8     bool *flag = new bool[n];
 9     flag[n-1] = true;
10     int i, j;
11     for (i=0; i<n-1; i++)
12       flag[i] = false;
13     for (i=n-2; i>=0; i--){
14       for (j=1; j<=A[i] && i+j<n; j++){
15         if (flag[i+j]){
16           flag[i] = true;
17           break;
18         }
19         else if (A[i]-A[i+j]<=j && flag[i+j]==false)
20           break;
21       }
22     }
23     bool rlt = flag[0];
24     delete[]flag;
25     return rlt;
26   }
27 };