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.

  1. class Solution {
  2. public:
  3. bool canJump(int A[], int n) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. if(==n) return true;
  7. if(==n) return false;
  8.  
  9. int position = ;
  10.  
  11. while(position != (n-)){
  12. if( == A[position])
  13. return false;
  14.  
  15. position += A[position];
  16. if(position >=n)
  17. return true;
  18. }
  19.  
  20. return true;
  21. }
  22. };

我的答案

思路:这道题由于说明了是non-negative integer,唯一到不了终点的情况就是踩到了值为0的点,不能往后走了。总感觉这道题目有问题,如果最后一步迈的特别大,以至于超过了数组的最大脚标,按online judge的意思是可以算作到达的,我原本理解成为了必须正好踩到最后一个位置上才算是到达~总觉得题目有点问题。

[leetcode.com]算法题目 - Jump Game的更多相关文章

  1. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  2. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  4. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  7. [leetcode.com]算法题目 - Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...

  8. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. [leetcode.com]算法题目 - Pow(x, n)

    Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...

随机推荐

  1. Ubuntu下Tomcat绑定80端口(zz)

    Ubuntu下Tomcat绑定80端口 来源:本站转载 作者:佚名 时间:2011-02-22 TAG: 工作环境迁移到了Ubuntu,很多东西发生了变化,比如原先配置tomcat端口.只需要配置se ...

  2. Reading CLR via c# 4th Edition

    In fact, at runtime, the CLR has no idea which programming language the developer used for thesource ...

  3. NOIP水题测试(2017082501)

    日常水题测试又来了! 以后答案都以单题形式公布. 下面看今天的水题: 时间限制:5小时 题目一:无法形容的水 题目二:比上一题还水 题目三:一元三次方程求解 题目四:单词接龙 题目五:统计单词个数 题 ...

  4. O365 Manager Plus详解

  5. 通过http.client解析url返回的数据时为什么中文变成了unicode码

    今天在解析json数据的时候得到了一堆这样的数据:{"errNum":0,"errMsg":"success","retData& ...

  6. char与CString相互转换

    Char -> CStringchar ch[] = "Hello";CString str;str.Format("%s",ch);CString -& ...

  7. php代码记录

    公司项目的随想记录也记在这里: 1,证书产生的目的是为了防止不合法的用户能够直接访问接口获取数据.证书由服务器端生成,然后返回给app.然后app拿着这个证书到服务器端获取接口数据,而不是app的合法 ...

  8. gj13 asyncio并发编程

    13.1 事件循环 asyncio 包含各种特定系统实现的模块化事件循环 传输和协议抽象 对TCP.UDP.SSL.子进程.延时调用以及其他的具体支持 模仿futures模块但适用于事件循环使用的Fu ...

  9. mysql学习之路_字段类型与属性2

    字段属性: 主键,唯一键,自增长. 主键: Primary key 主要的键,一张表只能有一个字段能使用对应的键,用来唯一约束该字段里面的数据不能重复,称之为主见. 一张表最多只有一个主键. 增加主键 ...

  10. DOM3级的变化

    由于存在跨浏览器开发问题所以不推荐使用: 兼容性: event.key 包含所按下键的字符 event.char 属性IE9和safari和chrome并不支持 event.location 返回所按 ...