leetcode—jump game
1.题目描述
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.
2.解法分析
如果数组中没有零,一定能到达最后一个元素,反之,如果有零,那么如果0前面的元素没有一个能直接跳到0后面的元素的话,肯定是不能到达最后的元素的。
class Solution {public:bool canJump(int A[], int n) {// Start typing your C/C++ solution below// DO NOT write int main() function//如果数组中没有0,肯定能到达int max=0;for(int i=0;i<n-1;++i){if(A[i]==0){if(i>=max)return false;else continue;}if((i+A[i])>max){max=i+A[i];if(max>=n-1)return true;}}return true;}};
leetcode—jump game的更多相关文章
- [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 ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]Jump Game @ Python
原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode: Jump Game Total 解题报告
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of ...
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- [LeetCode] Jump Game II 贪心
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 ...
- Leetcode jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Linux音频编程指南
Linux音频编程指南 虽然目前Linux的优势主要体现在网络服务方面,但事实上同样也有着非常丰富的媒体功能,本文就是以多媒体应用中最基本的声音为对象,介绍如何在Linux平台下开发实际的音频应用程序 ...
- 使用C#开发ActiveX控件(新) 转 http://www.cnblogs.com/yilin/p/csharp-activex.html
前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动态处理能力.通常ActiveX控件都是 ...
- HttpClient通过GET和POST获取网页内容
中国银行支付网关---银行回调的接口 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面 /** * 中国银行支付网关---银行回调的接口 * @svncode svn://10. ...
- JavaScript 中 2个等号(==)和 3个等号(===)之间的区别
JavaScript(JS)中有3个和等号(=)相关的操作符:赋值运算符(=).等于(==).恒等于(===). 赋值运算符不多说了. 这里说说等于和恒等于. ==,等于:两边值类型不同的时候,会自动 ...
- SQL Server Object Explorer in VS
菜单栏View-->SQL Server Object Explorer 默认有几个连接,可以根据需要自己再另外添加 比如添加127.0.0.1 建立连接之后,剩下的操作和sql server中 ...
- [HIHO1299]打折机票(线段树)
题目链接:http://hihocoder.com/problemset/problem/1299 线段树,按照t为下标去更新v,更新的时候要保留最大的那个. #include <algorit ...
- plsql programming 19 触发器
挂起语句, 是指数据库 Hang 到那不能动了, 触发的. 1. DML 触发器 这种类型的触发器对于开发人员都很常见, 其他类型的触发器主要是给DBA使用的. 配置触发器,我们需要回答以下问题: 触 ...
- HeadFirst 13 (包装器, 过滤器) not Finish
过滤器准许你拦截请求 容器管理过滤器的生命周期 都在DD中声明
- linux下gitflow辅助工具安装和使用
gitflow是一个确保nvie推荐的git branch分支策略最佳模型得到有效实施的辅助工具.它作为git的一个子命令而存在. http://nvie.com/posts/a-successful ...
- git cheatsheet小抄本
https://www.kernel.org/pub/software/scm/git/docs/git.html