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. Tensorflow学习笔记3:卷积神经网络实现手写字符识别

    # -*- coding:utf-8 -*- import tensorflow as tf from tensorflow.examples.tutorials.mnist import input ...

  2. Firewalld--03 富规则、备份恢复、开启内部上网

    目录 防火墙富规则.备份恢复.开启内部上网 1. 防火墙富规则策略 2.Firewalld备份恢复 3. 防火墙开启内部上网 防火墙富规则.备份恢复.开启内部上网 1. 防火墙富规则策略 ​ Fire ...

  3. 记一次 gunicorn 启动 flask 出问题的经历

    出错现象: gunicorn+nginx+flask 部署项目, 部署过程没问题,项目也正常启动了,但是一旦访问接口,就会报错: Traceback (most recent call last): ...

  4. Redis分布式锁【实战】

    概述 目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个分布式系统都无法同时满足一致性(Consistency).可 ...

  5. redis下载及安装教程

    https://blog.csdn.net/w546097639/article/details/88547486

  6. Idea配置注释

    Idea配置注释 方法注释 点击+号 选择2 template Group 自己随便填个有意义的name(如图的mn就是我填写的) 点击你上步填写的name (我的是mn),然后点击+选择1 Live ...

  7. php md5()函数 语法

    php md5()函数 语法 作用:字符串md5编码.dd马达价格 语法:md5(string,raw) 参数: 参数 描述 string     必需.规定要计算的字符串. raw     可选.规 ...

  8. 用soapUI开发webservice接口

    1,下载soapUI软件,安装到本地 2,打开soapUI软件 3,创建一个开发好的接口 4,进行接口调用 测试:

  9. 【Java】commons-lang3中DateUtils类方法介绍

    添加commons-lang3的Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <art ...

  10. Hadoop ”No room for reduce task“问题处理

    早上发现一个任务有20个reduce,但是只有四个正常完成,剩余16个等待了8个小时才分配执行(集群槽位资源充足) 解决方法:查看了集群的log,发现有这种warn: -- ::, WARN org. ...