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 ...
随机推荐
- 用Spring Data JPA 基于内存存储pojo的简单案例
poject结构如下: Customer.java类是一个pojo类,代码如下: package hello; import javax.persistence.Entity; import java ...
- 浅谈Linux下的五种I/O模型
一.关于I/O模型的引出 我们都知道,为了OS的安全性等的考虑,进程是无法直接操作I/O设备的,其必须通过系统调用请求内核来协助完成I/O动作,而内核会为每个I/O设备维护一个buffer.如下图所 ...
- HTTP头学习汇总
在开发http请求的时候,对HTTP头部信息一知半解,各种百度谷歌汇总一下学习到的资料. http简介 HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于 ...
- hibernate annotation配置经验
1.将annotation写在entity类文件的get方法上面
- PS:WINRAR制作32位安装程序和64位安装程序选项
32位 64位
- Linux diff patch
/***************************************************************************** * Linux diff patch * ...
- js屏弊错误
<SCRIPT language=javascript> <!-- window.onerror=function(){return true;} // --> </SC ...
- PPTP模式跟L2TP模式有什么不同
使用VPN的时候经常会看到商家说支持PPTP模式和L2TP模式,但是许多朋友都不知道有什么区别,该用哪一个,下面给你们讲讲: 1).PPTP协议是点对点隧道协议:其将控制包与数据包分开,控制包采用TC ...
- MySQL server has gone away 的解决方法
原文引用至:http://www.jb51.net/article/23781.htm,感谢! 可能原因:1.发送的SQL语句太长,以致超过了max_allowed_packet的大小,如果是这种原因 ...
- SQL server2012连接不上
数据库连接不上 其中一种可能的解决办法: 开始-所有程序-Microsoft SQL server 2012-配置工具-SQL Server 配置管理器-SQL server 服务-SQL serve ...