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

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

     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 false;
     8             
     9         int step = A[0];
    10         
    11         for(int i = 1; i < n; i++)
    12             if (step > 0)
    13             {
    14                 step--;
    15                 step = max(step, A[i]);
    16             }
    17             else
    18                 return false;
    19                 
    20         return true;
    21     }
    22 };
  • 相关阅读:
    openresty
    ATS 相关
    pandas
    flask
    ansible
    zipline
    bcolz
    数据分析 --- concat
    Go --- 基础使用
    Go --- 基础介绍
  • 原文地址:https://www.cnblogs.com/chkkch/p/2765894.html
Copyright © 2011-2022 走看看