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. 十大热门AI芯片

    资料来源:头条<人工智能影响力报告>中的人工智能十大热门芯片 iPhone X内部搭载了一颗全新定制的处理器——A11 Boinic,用来承担人脸识别和移动支付的工作负荷.双核心A11芯片 ...

  2. tmux 操作简版

    操作session: 操作window: 操作pane: 原文

  3. mvn 打包排除test

    mvn clean package compile -Dmaven.test.skip=true

  4. 【串线篇】spring boot外部配置加载顺序

    SpringBoot也可以从以下位置加载配置: 原则仍然是优先级从高到低:高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 java -j ...

  5. Linux下python pip手动安装笔记

    今天查问题, 从redis集群中模糊查询某个key, 用一些重复的命令, 链接不同的node, redis-cli 去查, 感觉不舒服. 考虑写一些shell或py来简化一下. 一查环境, 安装了py ...

  6. 对vue的研究

    beforeDestroy 类型:Function 详细: 实例销毁之前调用.在这一步,实例仍然完全可用. 该钩子在服务器端渲染期间不被调用. 参考:生命周期图示 destroyed 类型:Funct ...

  7. php中substr_compare()区分大小写吗

    PHP substr_compare() 函数 定义和用法 substr_compare() 函数从指定的开始位置比较两个字符串. 提示:该函数是二进制安全且选择性地对大小写敏感(区分大小写). 语法 ...

  8. [CF846E]Chemistry in Berland题解

    这题乍一看是一道水树形DP(其实事实上它确实是树形DP),然后设f[i]表示第i个点所多余/需要的材料,然后我们愉快的列出了式子: if(f[v]<0) f[u] += f[v] * edges ...

  9. springmvc上传文件异常

    症状: error:org.springframework.web.multipart.MultipartException: Current request is not a multipart r ...

  10. 【CF1243A】Maximum Square【贪心】

    题意:给你n个长度为ai的木板,求最大能拼成的矩形为多大 题解:显然贪心每次选最大的进去拼,那么剧需要枚举矩形长度x,看最长的k个能够拼出长度为x的矩形即可 #include<iostream& ...