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. 贪心思想
如果当前存的步数now 小于于当前位置可以跳的步数,则更新now now 小于0则死掉了。
 class Solution {
public boolean canJump(int[] nums) {
int now = nums[0];
for(int i =1;i<nums.length;i++){
now--;
if(now<0) return false;
if(nums[i]>now){
now = nums[i];
}
}
return true;
}
}

55. Jump Game(贪心)的更多相关文章

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

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

  2. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  3. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  4. 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 ...

  5. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  6. leetcode 55 Jump Game 三种方法,回溯、动态规划、贪心

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

  7. 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 解题思路

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

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

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

随机推荐

  1. Rsync未授权访问漏洞的利用和防御

    首先Rsync未授权访问利用 该漏洞最大的隐患在于写权限的开启,一旦开启了写权限,用户就可以,用户就可以利用该权限写马或者写一句话,从而拿到shell. 我们具体来看配置文件的网相关选项(/etc/r ...

  2. 【咸鱼教程】Wing动画编辑器创建精美(一般-_-)开场动画

    游戏中会用着一些简单的动画,公司一般使用的dragonbones制作,导出二进制格式或者MC来使用.感觉一些简单动画直接使用动画编辑器更加简便些. 引擎版本:5.0.14wing版本:4.1.0 一 ...

  3. java的前缀自增自减和后缀自增自减

    2.前缀自增自减法(++a,--a): 先进行自增或者自减运算,再进行表达式运算. 3.后缀自增自减法(a++,a--): 先进行表达式运算,再进行自增或者自减运算 实例: 实例 public cla ...

  4. Windows Server 2008 R2之五操作主控的管理

    一.概述 操作主控(FSMO)也称作操作主机(OM),它是指在AD中一个或多个特殊的DC,用来执行某些特殊的功能(资源标识符SID分配.架构修改.PDC选择等). 1.操作主控的分类 基于森林的操作主 ...

  5. 纯CSS绘制的三角形箭头图案【原创】

    参考:http://www.webhek.com/css-triangles/ 使用上下左右的三角形箭头标志,直接用css即可完成,直接附上代码. css: div#up { width: 0px; ...

  6. python pandas 豆瓣电影 top250 数据分析

    豆瓣电影top250数据分析 数据来源(豆瓣电影top250) 爬虫代码比较简单 数据较为真实,可以进行初步的数据分析 可以将前面的几篇文章中的介绍的数据预处理的方法进行实践 最后用matplotli ...

  7. Swift 使用 LLDB 调试命令

    swift 和 oc 的语法不一样: Xcode 调试技巧之 Swift 篇 打印和赋值,观察数值变量和view对象属性 p指令可打印其对象类型.内存地址以及该对象的值等具体信息, po指令则是打印其 ...

  8. 这个代码给所有带有name属性的链接加了一个背景色

    jQuery起点教程之使用选择器和事件jQuery提供两种方式来选择html的elements: 第一种是用CSS和Xpath选择器联合起来形成一个字符串来传送到jQuery的构造器(如:$(&quo ...

  9. CentOS 6U7分区大于2TB的磁盘以及挂载大于16TB分区磁盘的解决方案

    一.内容介绍1.问题描述1).问题一 CentOS 6.x 在格式化大于16TB的ext4分区时,会提示如下错误: mke2fs 1.41.12 (17-May-2010)mkfs.ext4: Siz ...

  10. J - Fire!---UVA 11624

    题目链接 题意:J代表Joe的位置,F代表火的起点,下一刻火将会向四周扩散,求Joe逃离的最短时间,如果不能逃离输出IMPOSSIBLE; 注意火的起点可能不止一处 可以用两次bfs分别求出人到达某个 ...