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.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

数组中每一个位置的数字代表可以往前跳跃的最大距离,判断根据数组中的数字能不能跳到最后一个位置

解法1:

设立一个标志位m表示在某个位置时候,获得的最大跳跃距离,如果m<i,则表示不能跳至此位置,失败

 class Solution(object):
def canJump(self, nums):
m = 0
for i,n in enumerate(nums):
if i>m:
return False
m = max(m,i+n)
return True

解法2:

从最后一个元素往前遍历,每到一个位置时候判断前面位置的元素加上其值可否到达此位置

 class Solution(object):
def canJump(self, nums):
goal = len(nums)-1
for i in range(len(nums)-1,-1,-1):
if i+nums[i]>=goal:
goal = i
return not goal

[leetcode greedy]55. Jump Game的更多相关文章

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

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

  2. 【一天一道LeetCode】#55. Jump Game

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

  3. 【LeetCode】55. Jump Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  4. LeetCode OJ 55. Jump Game

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

  5. [leetcode greedy]45. Jump Game II

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

  6. 【LeetCode】55. Jump Game

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

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

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

  8. 55. Jump Game leetcode

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

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

随机推荐

  1. [csp-201509-3]模板生成系统

    #include<bits/stdc++.h> using namespace std; ; string a[N],b[N],c[N]; int main() { //freopen(& ...

  2. 1.phpcms 伪静态

    location / { if (!-f $request_filename){ rewrite (.*) /index.php; } rewrite ^/caipu-([-]+)-([-]+)-([ ...

  3. HDU 1256 画8 模拟题

    解题报告:这题我觉得题目有一个没有交代清楚的地方就是关于横线的字符的宽度的问题,题目并没有说,事实上题目要求的是在保证下面的圈高度不小于上面的圈的高度的情况下,横线的宽度就是等于下面的圈的高度. #i ...

  4. P1879 [USACO06NOV]玉米田Corn Fields (状压dp入门)

    题目链接: https://www.luogu.org/problemnew/show/P1879 具体思路: 我们可以先把所有合法的情况枚举出来,然后对第一行判断有多少种情况满足,然后对于剩下的行数 ...

  5. Mysql中truncate table和delete语句的区别

    Mysql中的truncate table和delete语句都可以删除表里面所有数据,但是在一些情况下有些不同! 例子: truncate table gag; (1)truncate table删除 ...

  6. linux文件管理 -> 系统压缩打包

    如果希望windows和Linux互相能使用的压缩工具, 建议.zip格式 压缩的好处主要有: 节省磁盘空间占用率 节省网络传输带宽消耗 网络传输更加快捷 Linux系统常见的后缀名所对应的压缩工具 ...

  7. iframe内部刷新后跳转锚点

    开发过程中需要在iframe内容页中点击刷新按钮刷新页面并跳转至页面底部,编写js函数在url后面加上锚点名称#mao,但发现并未达到预期效果,通过测试发现锚点只有在第一次访问页面的时候才会生效,所有 ...

  8. VirtualBox上安装CentOS-7(Minimal)

    Windows 10家庭中文版,VirtualBox 5.2.12,CentOS 7(Minimal版), 因为听到大家在谈论CentOS,阿里云上也有CentOS,CentOS还是Red Hat出品 ...

  9. mybatis 控制台打印sql脚本

    在mybatis-config.xml文件中加一句 <setting name="logImpl" value="STDOUT_LOGGING" /> ...

  10. thinkphp5高亮当前页(仅针对个人项目记录,不做通用参考)

    <div class="navbg"> <ul class="menu"> <li> <a href="/& ...