zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    随笔
    随笔
    第一个存储过程
    mysql 存储过程
    join
    随笔
    玩家注册登录
    mysql 存储二进制数据
    mysql学习
    socket listen/accept
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2854038.html
Copyright © 2011-2022 走看看