Jump Game - LeetCode
题目链接
注意点
解法
解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能。并且如果i超过了能到达的最大距离说明不能到达,因为i是每次加一都能超过最大距离,小于i的所有位置都会走到某个最远距离为0的位置。时间复杂度O(n)
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),maxPos = 0;
for(int i = 0;i <= maxPos;i++)
{
if(maxPos >= n-1) return true;
maxPos = max(maxPos,i+nums[i]);
}
return false;
}
};

解法二:网上看来的解法 —— 动态规划。但是我并不能理解这个状态转移方程dp[i] = max(dp[i - 1], nums[i - 1]) - 1
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(),pre = nums[0];
for(int i = 1;i < n;i++)
{
pre = max(pre,nums[i-1])-1;
if(pre < 0) return false;
}
return true;
}
};

小结
- 希望有大神可以详说一下解法二的思路 TAT
Jump Game - LeetCode的更多相关文章
- 55. Jump Game leetcode
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
- Jump Game —— LeetCode
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Jump Game leetcode java
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- php快速上手总结
PHP作为现代热门主流的开发语言,对于那些想加入新手PHPer,从哪学起,如何学习?你必须要需要掌握PHP的基础知识,基础知识相当于重点,是不可忽视的知识.常用的功能模块,面向对象的,MVC等相关技能 ...
- k8s踩坑记第1篇--rc无法创建
六一快乐!!! 什么是k8s,我不想解释,百度资料有很多,本系列只踩坑,不科普. 问题描述: 做Hello World的例子,结果get pods一直显示没有资源? 应用配置代码: apiVersio ...
- python2.7 倒计时
From: http://www.vitostack.com/2016/06/05/python-clock/#more Python公告 Python 发布了一个网站 http://pythoncl ...
- Alpha发布PSP Daily评价总结报告
Alpha发布PSP Daily评价总结报告 优点: 1.用户人群较为明确,定位较为准确. 2.亮点:暂停任务时是无法结束当前任务的. 3.说明书写的详细.语言流畅.能实现的功能都体现出来. 4.下拉 ...
- <构建之法>阅读感想
在阅读<构建之法>之前,我所认为的软件就是通过c,c++等语言编程,制作出的一个能满足人们操作需求的一些代码,认为一个好的软件工程师,就是能够在很短的时间之内,最快的根据需求写出几段代码程 ...
- 在新的电脑上部署 Hexo,保留原有博客的方法
用U盘从旧的电脑拷贝整个blog文件夹. 在新的电脑上装好git并配置好用户名和密钥. 安装 node.js 安装 hexo:npm install hexo-cli -g 用U盘把blog文件夹拷贝 ...
- Spring 中使用Properties文件
Spring提供了加载Properties文件的工具类:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. ...
- 关闭Centos5.5的写磁盘I/O功能
一个Linux文件默认有3个时间. atime:对此文件的访问时间. ctime:此文件inode发生变化的时间. mtime:此文件的修改时间. 如果有多个小文件(比如Web服务器的页面上有多个小图 ...
- Excel作为数据源TesTNG做数据驱动完整代码
说明:EXCEL 支持xls 和xlsx 俩种格式 : 已经过测试 ! package main.java; import org.apache.poi.ss.usermodel.*; import ...
- 关于JEE web项目 Servlet中 “/” 的解释 ;
1.关于"/" 可以代表web应用的根目录,也可以代表站点的根目录: 1>如果交给浏览器解析,则代表web站点的根目录,如果交给web服务器解析则代表项目的根目录: 2> ...