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. "不能将值 NULL 插入列 'ID',表 列不允许有 Null 值."

    问题: "不能将值 NULL 插入列 'ID',表 列不允许有 Null 值." 原因: 在进行表创建的时候没有将主键自增字段添加标识. 在使用navicat进行表创建的时候一定要 ...

  2. zabbix 4.2 发送警告邮件Python脚本

    #!/usr/bin/env python#-*- coding: UTF-8 -*-import os,sysimport getoptimport smtplibfrom email.MIMETe ...

  3. MYSQL学习笔记——数据类型

    mysql的数据类型可以分为三大类,分别是数值数据类型.字符串数据类型以及日期时间数据类型. 数值数据类型                                               ...

  4. Java技术中如何使用keepalived实现双机热备

    Keepalived简介 Keepalived是Linux下一个轻量级别的高可用解决方案.高可用(High Avalilability,HA),其实两种不同的含义:广义来讲,是指整个系统的高可用行,狭 ...

  5. margin 和padding 的区别

    margin是用来隔开元素与元素的间距:padding是用来隔开元素与内容的间隔.margin用于布局分开元素使元素与元素互不相干: padding用于元素与内容之间的间隔,让内容(文字)与(包裹)元 ...

  6. springboot+thymeleaf 访问静态资源解决(static)

    ---------------------------------------2.1.3-------------------------------------------------------- ...

  7. Dubbo 在跨语言和协议穿透性方向的探索:支持 HTTP/2 gRPC

    Dubbo 在跨语言和协议穿透性方向上的探索:支持 HTTP/2 gRPC 和 Protobuf 本文整理自刘军在 Dubbo 成都 meetup 上分享的<Dubbo 在多语言和协议穿透性方向 ...

  8. redis学习 --List

    1:添加   lpush    rpush     如果没有key的话,会报错.   lpushx    rpushx    如果没有key的话,不做任何反应. 2:得到 lpop   rpop   ...

  9. java实现js端的escape和unescape

    1.今天遇到这么个问题,需要把一些特殊字符传递到后台进行处理,例如Aa111111!@#,结果到了后台出现了个别字符中文符号了.这个时候需要转码.常见的就是js端的escape和unescape这种函 ...

  10. 理解性能的奥秘——应用程序中慢,SSMS中快(4)收集解决参数嗅探问题的信息

    ---从计划缓存中直接获取查询计划和参数: ), ) SELECT @dbname = 'hydee_连锁', @procname = 'dbo.p_select_ware'; WITH baseda ...