主要有两种思路:

一、

本题只需要判断能否到达最后一个数,那么可以采用贪心策略,到达某个位置i后,在直接在这个位置的基础上走nums[i]步,主要保证能一直前进,就能达到终点;

那么,什么时候才不能一直前进呢?

答案是,遇到0的时候,遇到nums[i]=0的时候,只要我们想办法跳过这个0,那么就可以确保我们可以继续前进。

所以遇到0时的处理方法是,往回搜索,设当前的位置为pos,即nums[pos]=0,一直搜索之前的数,判断nums[i]+i是否大于nums[pos],大于则可以继续上述的贪心算法,假如一直跳不过0(即搜索到i=0仍跳不过pos),则return false。

代码如下:

class Solution {
public:
bool canJump(vector<int>& nums) {
if(nums.size()==1) return true;
int pos=0;
while(pos<nums.size()-1)
{
if(nums[pos]!=0)
{//走最远的位置
pos=nums[pos]+pos;
}
else
{//如果最远的位置为0
int now=pos;
for(int i=pos;i>=0;i--)
{//找到能跳过0的位置
if(i+nums[i]>now)
{
pos=i+nums[i];
break;
}
}
if(pos==now)
{//找不到跳过0,则false
return false;
}
}
}
return true;
}
};

二、

记录两个变量,一个是当前能达到的最远距离,另外一个是下一次能达到的最远距离。

O(n)的时间复杂度和O(1)的空间复杂度。

在到达当前最远距离前(包括当前最远距离),不断更新下一次能达到的最远距离。当到达了当前最远距离,除了更新nextMaxPos,还需要把currentMaxPos=nextMaxPos,nextMaxPos=0

下面为AC代码:

class Solution {
public:
bool canJump(vector<int>& nums) {
if(nums.size()==1) return true;
int currentMaxPos=nums[0];
int nextMaxPos=0;
for(int i=0;i<nums.size();i++)
{
if(i<currentMaxPos)
{//更新下一次能达到的最远距离
nextMaxPos=max(nextMaxPos,i+nums[i]);
if(nextMaxPos>=nums.size()-1) return true;
}
else if(i==currentMaxPos)
{//达到当前最远的位置了,更新下一次能到达的最远距离,并且更新currentMaxPos和NextMaxPos
nextMaxPos=max(nextMaxPos,i+nums[i]);
if(nextMaxPos>=nums.size()-1) return true;
currentMaxPos=nextMaxPos;
nextMaxPos=0; }
else
{//更新当前能到的最远位置,把下一次能达到的最远距离清零
currentMaxPos=nextMaxPos;
nextMaxPos=0;
}
}
if(nextMaxPos>=nums.size()-1||currentMaxPos>=nums.size()-1) return true;
else return false;
}
};

Jump Game (Medium)的更多相关文章

  1. [array] leetcode-55. Jump Game - Medium

    leetcode-55. Jump Game - Medium descrition Given an array of non-negative integers, you are initiall ...

  2. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  5. [LeetCode] 系统刷题5_Dynamic Programming

    Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...

  6. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  7. [LeetCode] questions conclusion_ Dynamic Programming

    Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

随机推荐

  1. UML-架构分析-架构原则

    1.高内聚 2.低耦合 3.防止变异(间接性等) 4.关注点分离 方法1: 事物模块化,封装到单独的子系统中 方法2: 装饰者模式 方法3: 面向方面(AOP)

  2. 51nod A 魔法部落(逆元费马小定理)

    A 魔法部落 小Biu所在的部落是一个魔法部落,部落中一共有n+1个人,小Biu是魔法部落中最菜的,所以他的魔力值为1,魔法部落中n个人的魔法值都不相同,第一个人的魔法值是小Biu的3倍,第二个人的魔 ...

  3. 并发与高并发(七)-线程安全性-原子性-atomic

    一.线程安全性定义 定义:当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些线程将如何交替执行,并且在主调代码中不需要任何额外的同步或协同,这个类都能表现出正确的行为,那么就称这个类是线程 ...

  4. docker安装fastdfs与java客户端测试

    一.docker 安装FastDFS 1.拉取镜像 docker pull morunchang/fastdfs 2.创建并启动tracker容器 docker run -d --name=track ...

  5. Python笔记_第四篇_高阶编程_正则表达式_3.正则表达式深入

    1. re.split 正则的字符串切割 str1 = "Thomas is a good man" print(re.split(r" +",str1)) # ...

  6. uni-app文章详情-富文本展示 优雅展示代码块

    在uni-app开发中,开发一个资讯详情页面,详情里包含图片和代码块.这时候用简单的rich-text控件已经不够用了.用官方demo里的html-parser.js也无法很好的展示代码区域.这个时候 ...

  7. psmisc

    https://packages.ubuntu.com/xenial/psmisc Package: psmisc (22.21-2.1build1) Other Packages Related t ...

  8. Go-简介-发展

    01-Go语言介绍 目录 Go语言介绍 Go语言特性 Go语言发展(版本/特性) Go语言应用 谁在用 应用领域 Go语言项目 Go语架构 Go语言发展前景 Go语言介绍 Go 即Golang,是Go ...

  9. matlab代码学习_2018-7-28

    1.核范数||A|| * 是指矩阵奇异值的和,英文称呼叫Nuclear Norm.matlab code:[s, u, v] = svd(A); nulear_norm = sum(diag(s)); ...

  10. ES6之展开运算符

    本文介绍ES6新增的展开运算符(spread operator). 由上图可得,展开运算符负责拼装数组和对象,与之相反,解构赋值负责分解数组和对象. 由上图可得,展开运算符能和解构赋值一起发挥成更大的 ...