转自: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

相关文章:

  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-14
  • 2021-06-19
  • 2021-06-23
  • 2021-10-30
  • 2021-08-15
  • 2022-01-05
相关资源
相似解决方案