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 ...
随机推荐
- [原]携程预选赛A题-聪明的猴子-GCD+DP
题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- pythonweb自动化测试
from selenium import webdriverimport time def capture(url, save_fn="capture.png"): browser ...
- [HDOJ2602]Bone Collector(01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 裸的... #include <algorithm> #include <io ...
- UESTC 1074 秋实大哥搞算数 栈模拟
秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- JSON 之 SuperObject(5): Format 与转义字符
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- bzoj4197
这题现场想的思路方向都是对的,但限于现场和实力因素没能A 很显然我们会想到质因数的选取 如果某个质数p被W选了,那G就不能选含有质因子p的数 因此我们不难想到状压质数的选取情况,令f[i][j]为w质 ...
- jquery仿天猫商城左侧导航菜单
之前看到有博友写了一个仿天猫商城左侧导航菜单,可惜不提供免费下载,也没有代码.以前自己也写过类似的效果,只是都是一小块一小块的,现在重新拼凑.我将一步一步的实现拼凑过程,希望对你有所帮助. Demo在 ...
- 浅谈 Scala 中下划线的用途
Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...
- 未能加载文件或程序集“XXXXXX”或它的某一个依赖项。试图加载格式不正确的程序。
在本机WIN7机器上的WebService部署到Win2008R2上发现错误 “/”应用程序中的服务器错误. 未能加载文件或程序集“XXXXXX”或它的某一个依赖项.试图加载格式不正确的程序. 说明: ...
- BZOJ 1010 玩具装箱
斜率优化. 事实上是选一个大于某个数的最小斜率.维护下凸壳. #include<iostream> #include<cstdio> #include<cstring&g ...