一. 题目描写叙述

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下ifconfig不显示ip地址问题总结

    问题一:ifconfig之后只显示lo,没有看到eth0 ? eth0设置不正确,导致无法正常启动,修改eth0配置文件就好 ubuntu 12.04的网络设置文件是/etc/network/inte ...

  2. python3 操作excel表

    python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库可从这里下载https://pypi.python.org/pypi.下面分别记录py ...

  3. SpringBoot中如何使用jpa和jpa的相关知识总结

    jpa常用的注解: 注解 解释 @Entity 声明类为实体或表. @Table 声明表名. @Basic 指定非约束明确的各个字段. @Embedded 指定类或它的值是一个可嵌入的类的实例的实体的 ...

  4. 从输入URL到网页呈现的过程

    1.域名解析当我们在浏览器中输入一个URL,例如”www.google.com”时,这个地址并不是谷歌网站真正意义上的地址.互联网上每一台计算机的唯一标识是它的IP地址,因此我们输入的网址首先需要先解 ...

  5. libevent reference Mannual IV --Helper functions and types

    FYI: http://www.wangafu.net/~nickm/libevent-book/Ref5_evutil.html Helper functions and types for Lib ...

  6. 部署live555到云

    1.下载live555源码:    wget http://www.live555.com/liveMedia/public/live.2017.10.28.tar.gz    2.解压源码包:   ...

  7. 【Codeforces 489D】Unbearable Controversy of Being

    [链接] 我是链接,点我呀:) [题意] 让你找到(a,b,c,d)的个数 这4个点之间有4条边有向边 (a,b)(b,c) (a,d)(d,c) 即有两条从a到b的路径,且这两条路径分别经过b和d到 ...

  8. 基础算法(java版本)

    Practice Author: Dorae Date: 2018年10月11日13:57:44 转载请注明出处 具体代码请移步git 基础算法 图 Prim Kruskal Dijkstra Flo ...

  9. kendo grid dropdownlist 联动 cascading

    之前是无法联动的 后来将html页面中的 //$('<input required data-text-field="CompanyName" data-value-fiel ...

  10. Oracle怎么用(常用工具)

    ​ Oracle数据库管理系统装好了!那要怎么用呢? 将介绍的工具:①Database Configuration Assistant ②SQL Plus ③SQL Developer ​ 一.Dat ...