[leetcode.com]算法题目 - Jump Game
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
.
- class Solution {
- public:
- bool canJump(int A[], int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- if(==n) return true;
- if(==n) return false;
- int position = ;
- while(position != (n-)){
- if( == A[position])
- return false;
- position += A[position];
- if(position >=n)
- return true;
- }
- return true;
- }
- };
我的答案
思路:这道题由于说明了是non-negative integer,唯一到不了终点的情况就是踩到了值为0的点,不能往后走了。总感觉这道题目有问题,如果最后一步迈的特别大,以至于超过了数组的最大脚标,按online judge的意思是可以算作到达的,我原本理解成为了必须正好踩到最后一个位置上才算是到达~总觉得题目有点问题。
[leetcode.com]算法题目 - Jump Game的更多相关文章
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode.com]算法题目 - Pow(x, n)
Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...
随机推荐
- 事务ACID如何定义,事务隔离性解决的问题
挚享科技 2018.4.8 事务的四个特性: 1. 原子性: 同一个事务的多个操作,要么都成功,要么全部失败回滚. 2. 一致性: 事务必须确保数据库从一个一致性状态变换为另一个一致性状态. 其实就是 ...
- tomcat 配置域名访问应用
<Host appBase="webapps" autoDeploy="true" name="www.XXX.com" unpack ...
- SQL判断如果一列值为null则取另一列值代替 isnull()
[chClientCode] ,[nvcClientName] ,[chRegionCode] ,isnull(chUltimateHeadClientCode,[chClientCode]) as ...
- H3 android 系统编译
http://bbs.ickey.cn/group-topic-id-57981.html [Orange Pi PC试用体验]11编译android源码笔记 编译android和编译linux有点类 ...
- mysql练习题3
USE day44; -- 1 查出所有员工的名字,薪资,格式为 -- <名字:egon> <薪资:3000> SELECT '姓名:',name,'薪资:',salary f ...
- mybatis 使用merge into
前一篇博客,oracle的merge into语法 : oracle merge into语法 mybatis 使用merge into,跟一般的update写法相同: <update id=& ...
- 1-9-假期训练心得(dp+bfs)
题目一:传送门 思路:就是简单的bfs,注意仔细审题,加上对转弯次数的判断. 题目二:传送门 思路:简单dp,记录每一秒每个位置接到的大饼的数量. 状态转移方程:dp[i][j]=max(dp[i][ ...
- java sigar.jar
http://blog.csdn.net/yin_jw/article/details/40151547 DEBUG Sigar - no libsigar-x86-linux.so in java. ...
- Tomcat架构解析(三)-----Engine、host、context解析以及web应用加载
上一篇博文介绍了Server的创建,在Server创建完之后,就进入到Engine的创建过程,如下: 一.Engine的创建 1.创建Engine实例 当前次栈顶元素为Service对象,通过Se ...
- c语言const和c++const
1.常量 常量是指值不能被改变的量,又叫做字面值 1.1常量分类 1)字符常量:'a', 'A', '*'. 2)字符串常量:"helloworld","ilovechi ...