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. 为Docker容器中运行的gitlab添加ssh的一些问题记录

    最近做的一个东西,是将gitlab10.x的汉化版本,从源码编译(在源码中自己定制一些东西),然后制作成Docker镜像,作为Docker容器来运行 在启用容器中的gitlab的ssh的时候,遇到了一 ...

  2. ssh免密码登录之ssh-keygen的用法

    A服务器:192.168.100.2 B服务器:192.168.100.3 要达到的目的:A服务器ssh登录B服务器不需要输入密码 1.在A服务器下使用ssh-keygen命令生成公钥/私钥密钥对,执 ...

  3. 【转】RTMP/RTP/RTSP/RTCP协议对比与区别介绍

    用一句简单的话总结:RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步. 之所以以前对这几个有点分不清,是因为CTC标准里没有对RTCP进行要求,因此在标准RTSP的代码 ...

  4. 用js内置对象XMLHttpRequest 来用ajax

    步骤: /* 用XMLHTTPRequest来进行ajax异步数据交交互*/ 主要有几个步骤: //1.创建XMLHTTPRequest对象 //最复杂的一步 if (window.XMLHttpRe ...

  5. linux安装环境

    我用的是Linux ubuntu 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 x86_64 ...

  6. vue--todolist的实现

    简单示例: <template> <div id="Home"> <v-header></v-header> <hr> ...

  7. 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...

  8. HDU 2087 - 剪花布条 - [KMP算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...

  9. Fata7y Ya Warda! SPOJ - DRUIDEOI 单调栈

    题意:1e5个数围成一个环.现在要输出每个数左右第一个大于它的数的下标.若没有,则输出-1. 题解:单调栈板题.只是要把数据压入栈压两遍来模仿环.    具体分析:考虑一个递减的数列.要找左边最大的 ...

  10. Unified Modeling Language

    https://en.wikipedia.org/wiki/Unified_Modeling_Language