[leetcode]Jump Game

转自:http://www.cnblogs.com/remlostime/archive/2012/11/12/2765894.html

用贪心策略,刚开始step = A[0],到下一步step--, 并且取step = max(step, A[1]),这样step一直是保持最大的能移动步数,局部最优也是全局最优。

class Solution {
 public:
     bool canJump(int A[], int n) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         if (n == 0)
             return false;
             
         int step = A[0];
         
         for(int i = 1; i < n; i++)
             if (step > 0)
             {
                 step--;
                 step = max(step, A[i]);
             }
             else
                 return false;
                 
         return true;
     }
 };

EOF

原文地址:https://www.cnblogs.com/lihaozy/p/2854038.html