【题目】

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. legend2---开发日志9(vue常见无法自动更新改变的原因是什么)

    legend2---开发日志9(vue常见无法自动更新改变的原因是什么) 一.总结 一句话总结:没找到变量,比如在computed属性中vue的变量没加this 没找到变量 1.函数中var bott ...

  2. c# datagridview绑定数据源(BindingList<class>)中的现象 待查

    现象1: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  3. ggplot2画图

    早在N年前就听说这个包画图不错,一直没机会用,终于等到了.相比前面trendline这个包的可视化功能强大得多. ggplot2需要使用dataframe,其实就是一个N维数组, install.pa ...

  4. SOCKET 接收图片

    using System;using System.Collections.Generic;using System.Text;using System.Net.Sockets;using Syste ...

  5. 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建异步树形菜单

    jQuery EasyUI 树形菜单 - 创建异步树形菜单 为了创建异步的树形菜单(Tree),每一个树节点必须要有一个 'id' 属性,这个将提交回服务器去检索子节点数据. 创建树形菜单(Tree) ...

  6. You Don't Know JS: this & Object Prototypes( 第2章 this)

    this is a binding made for each function invocation, based entirely on its call-site (how the functi ...

  7. Confluence 6 空间

    什么是一个空间? Confluence 空间是包含有页面和博客页面的容器.你也可以将空间认为是对你工作中可以使用的 2 中类型的目录. 在 Confluence 中有下面 2 种空间类型: 站点空间( ...

  8. Being a Good Boy in Spring Festival HDU - 1850

    桌子上有M堆扑克牌:每堆牌的数量分别为Ni(i=1…M):两人轮流进行:每走一步可以任意选择一堆并取走其中的任意张牌:桌子上的扑克全部取光,则游戏结束:最后一次取牌的人为胜者. 现在我们不想研究到底先 ...

  9. 百度基础架构组-实习生面试(2016.08 java后台开发)

    一.项目 1.Spring MVC与Struts2的区别: 2.MVC三层是如何工作的?比如:要访问一个Url?a=xx&b=xx,怎么找到相应的资源,怎么运算,怎么返回等? 3.数据库myb ...

  10. [CodeForces - 614C] C - Peter and Snow Blower

    C - Peter and Snow Blower Peter got a new snow blower as a New Year present. Of course, Peter decide ...