【题目】

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

【思路】

贪心、取最大

【代码】

倒序考虑。设flag在尾部,一直考虑前面一步i,nums[i]+i>=flag,那i点可以到达flag点,则flag回跳到i点。

当循环结束,flag跳到起点0,则返回true。

class Solution {
public boolean canJump(int[] nums) {
int flag=nums.length-1;
for(int i=nums.length-1;i>=0;i--){
if(nums[i]+i>=flag)
flag=i;
}
return flag==0;
}
}

【其他】

public boolean canJump(int[] nums) {

  int tmp = 0;
  for (int i=0; i<nums.length; ++i) {
    if (i > tmp) return false;
    tmp = Math.max(tmp, i + nums[i]);
  }
  return true;
}

[Leetcode 55]跳格子JumpGame的更多相关文章

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

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

  2. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  3. [leetcode]55.JumpGame动态规划题目:跳数游戏

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

  4. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

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

  5. [LeetCode]55. 跳跃游戏(贪心)

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

  6. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

  7. LeetCode 55. Jump Game (跳跃游戏)

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

  8. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  9. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

随机推荐

  1. Java操作Kafka

    java操作kafka非常的简单,然后kafka也提供了很多缺省值,一般情况下我们不需要修改太多的参数就能使用.下面我贴出代码. pom.xml <dependency> <grou ...

  2. English trip V1 - 19.Where Am I? 我在哪里?Teacher:Patrick Key:Ask for and directions

    In this  lesson you will learn to ask for and give directions. 本节课你将学习到学会问路和指路. 课上内容(Lesson) 人类的几种感: ...

  3. 暂时关闭 windows 病毒防护

  4. hdu5992 kdt

    题意:n个旅馆,每个有花费,m个查询,查询在某个点在c花费范围内的距离最小的旅馆 题解:kdt,建成四维,坐标两维,花费一维,id一维,实际上建树只用前两维,正常的查询,如果满足条件在更新答案即可 / ...

  5. 【Java】【6】JDK8 Stream操作整理

    摘要: 1,List<EntityOld>转换为List<EntityNew> List<EntityOld> list = oldList; List<En ...

  6. 笔记react router 4(四)

    看完Router的变化,接着来说<Switch>组件. 在3.X中,你可以指定很多子路由,但是只有第一个匹配的路径才会被渲染. 就像这样, <Route path='/' compo ...

  7. 不安装Oracle数据库使用plsqldevloper

    1.Oracle官网下载instantclient 解压到D:\zl\instantclient_11_2 2.配置环境变量 ORACLE_HOME = D:\zl\instantclient_11_ ...

  8. SQL - 常用的特殊查询

    sql 查询某字段不重复的全部记录: select * from table where fid in(Select min(fid) FROM table group by name) :

  9. Linux Shell获取系统资源使用百分比(CentOS)

    CPU使用率: top -b -n | | 内存使用率: free -m | grep '^-' | awk '{print $3/($3+$4)*100"%"}' IO使用率(F ...

  10. windows工具打开命令

    程序 命令 位置 记事本 notepad C:\Windows\system32 ping ping C:\Windows\System32 服务管理器 services.msc C:\Windows ...