Level:

  Medium

题目描述:

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.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
jump length is 0, which makes it impossible to reach the last index.

思路分析:

  要想能够走到最后一个元素,则从头开始遍历,变量reach实时更新所能走的最大步数,判断能走的最大步数reach是否大于i,如果能则继续往下走,更新reach变量。

代码:

public class Solution{
public boolean canJump(int []nums){
int reach=0;
for(int i=0;i<nums.length;i++){
if(i>reach)
return false;
reach=Math.max(reach,i+nums[i]); //更新最大还能走几步
}
return true;
}
}

33.Jump Game(跳步游戏)的更多相关文章

  1. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. 【LeetCode每天一题】Jump Game(跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [LeetCode] 55. Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. 055 Jump Game 跳跃游戏

    给定一个非负整数数组,您最初位于数组的第一个索引处.数组中的每个元素表示您在该位置的最大跳跃长度.确定是否能够到达最后一个索引.示例:A = [2,3,1,1,4],返回 true.A = [3,2, ...

  5. Leetcode55. Jump Game跳跃游戏

    给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...

  6. [LeetCode] 45. Jump Game II 跳跃游戏 II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. LeetCode(45): 跳跃游戏 II

    Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  8. Unity 2D游戏开发教程之摄像头追踪功能

    Unity 2D游戏开发教程之摄像头追踪功能 上一章,我们创建了一个简单的2D游戏.此游戏中的精灵有3个状态:idle.left和right.这看起来确实很酷!但是仅有的3个状态却限制了精灵的能力,以 ...

  9. HTML5 2D平台游戏开发#2跳跃与二段跳

    在上一篇<Canvas制作时间与行为可控的sprite动画>中已经实现了角色的左右移动,本篇继续实现角色的一系列动作之一:跳跃.先来看看最终效果: 要实现跳跃,必须模拟垂直方向的速度和重力 ...

随机推荐

  1. UNIX网络编程总结一

    客户与服务器通信使用TCP在同一网络通信时,大致按下面的方式通信:client→TCP→IP→以太网驱动程序→以太网→以太网驱动程序→IP→TCP→server.若不在同一网络则需要路由器连接. 客户 ...

  2. alert(1) to win 6

    function escape(s) { // Slightly too lazy to make two input fields. // Pass in something like " ...

  3. CentOS7 利用systemctl添加自定义系统服务

    一.命令systemctl介绍 CentOS 7.0中已经没有service命令,而是启用了systemctl服务器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 命 ...

  4. mac os安裝jdk

    下載安裝 打开mac笔记本,输入账号密码登陆后,点击桌面上的terminal终端图标.打开终端,然后在终端中输入命令java.   从下面的图中可以看到,终端会自动给出提示,没有可以使用的java命令 ...

  5. 远程连接Linux相关操作

    1.远程连接的步骤 编译网卡配置文件,修改onboot参数 vi /etc/sysconfig/network-scripts/ifcfg-ens33进入命令之后将onboot改成onboot=yes ...

  6. [洛谷P1709] 隐藏的口令

    问题描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并 ...

  7. python的可视化展示(待更新)

    参考:https://www.cnblogs.com/zhizhan/p/5615947.html 通过matplotlib进行绘图: 例1: import matplotlib.pyplot as ...

  8. Jenkines邮件中添加图片

    1.在Jenkins的邮件插件 Email-ext中的Default Content内容编写html文件,简单模板如下: <html>  <head>  </head&g ...

  9. 微信公众号开发(二)获取access_token

    参考:https://www.cnblogs.com/liuhongfeng/p/4848851.html 一:介绍. 接口调用请求说明 http请求方式: GET https://api.weixi ...

  10. cenos中的软件安装

    在linux中安装flash:  http://jingyan.baidu.com/article/fa4125accdeeec28ad709252.html linux java环境的搭建: