一. 题目描写叙述

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.

二. 题目分析

该题的大意是,给定一个数组,从第一个元素開始,元素的值表示可以往后跳的最大距离,问依照这样的规则,该数组能否跳到最后一个元素。

解题的基本思路是贪心算法。当然。使用动态规划也是全然可以解决的,也贴出网上一种动规代码。

三. 演示样例代码

// greedy algorithm
class Solution {
public:
bool canJump(int A[], int n) {
if(n == 0 || n == 1){
return true;
}
int maxStep = A[0];
for(int index = 1 ; index < n ; ++index)
{
if(maxStep == 0) return false;
--maxStep;
if(maxStep < A[index])
maxStep = A[index];
if(maxStep + index >= n - 1) // 满足条件
return true;
}
}
};
// DP algorithm
class Solution {
public:
bool canJump(int A[], int n)
{
vector<int> f(n, 0);
f[0] = 0;
for (int i = 1; i < n; i++)
{
f[i] = max(f[i - 1], A[i - 1]) - 1;
if (f[i] < 0) return false;
}
return f[n - 1] >= 0;
}
};

四. 小结

该题的思路是,使maxStep一直保持最大的能移动步数。

leetcode笔记:Jump Game的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  10. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

随机推荐

  1. linux使用crontab实现PHP执行计划定时任务

    linux使用crontab实现PHP执行计划定时任务 前几天写过一篇文章,利用单纯的php实现定时执行任务,但是效率不佳,对于linux来说用crontab实现更加合理 首先说说cron,它是一个l ...

  2. 通过acdbblockreference 获得块名

    AcDbBlockReference *pBlkRef = AcDbBlockReference::cast(ent.object());     AcDbObjectId pBlkTblRecId; ...

  3. 如何在网页中浏览和编辑DWG文件 梦想CAD控件

    如何在网页中浏览和编辑DWG文件 梦想CAD控件 www.mxdraw.com 梦想绘图控件5.2  是国内最强,最专业的CAD开发组件(控件),不需要AutoCAD就能独立运行.控件使用VC 201 ...

  4. 使用Sophus练习李群SO3、SE3以及对应的李代数so3、se3

    这是高博<视觉SLAM14讲,从理论到实践>第4章的练习.加了一些注释和理解: #include <iostream>#include <cmath>using n ...

  5. A3. JVM 类加载器

    [概述] 虚拟机设计团队把类加载阶段中的 “通过一个类的全限定名来获取描述此类的二进制字节流” 这个动作放到 Java 虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代码 ...

  6. 【 jquery 】常用

    $("#input1").show('slide');    渐进显示$("#input1").hide('slide');     渐进隐藏 siblings ...

  7. 原来 JS 是这样的 - 关于 this

    引子 习惯了别的语言的思维习惯而不专门了解 JavaScript 的语言特性的话,难免踩到一些坑. 上一篇文章 中简单总结了关于 提升, 严格模式, 作用域 和 闭包 的几个常见问题,当然这仅仅是了解 ...

  8. 常用HTML5代码片段

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. <MySQL>入门六 变量

    /* 变量 系统变量: 全局变量 会话变量 自定义变量 用户变量 局部变量 */ -- ------------系统变量-------------------- /* 变量由系统提供,不是用户定义,属 ...

  10. BZOJ 4819 Luogu P3705 [SDOI2017]新生舞会 (最大费用最大流、二分、分数规划)

    现在怎么做的题都这么水了.. 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=4819 (luogu) https://ww ...