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. 当python模式遇见cedet

    TAG: emacs, python, cedet, semantic, ctags DATE: 2013-08-20 我用Emacs 24写python程序. 发现屏幕不时有些闪动,MiniBuff ...

  2. CVE-2017-12149JBoss 反序列化漏洞利用

    CVE-2017-12149 漏洞描述 互联网爆出JBOSSApplication Server反序列化命令执行漏洞(CVE-2017-12149),远程攻击者利用漏洞可在未经任何身份验证的服务器主机 ...

  3. Go语言的各种Print函数

    Go语言的各种Print函数 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) func Pr ...

  4. css3兼容性问题归纳

    Android2.3的overflow问题 在android2.3及以下系统版本的浏览器不支持overflow:scroll / auto,即在页面元素里面的内容如果超过了父元素或祖先元素的高度是无法 ...

  5. mybatis模糊查询防止SQL注入

    SQL注入,大家都不陌生,是一种常见的攻击方式.攻击者在界面的表单信息或URL上输入一些奇怪的SQL片段(例如“or ‘1’=’1’”这样的语句),有可能入侵参数检验不足的应用程序.所以,在我们的应用 ...

  6. XXX变种-防火墙放行自身

    1.利用防火墙命令放行自身手法 netsh firewall add allowedprogram "C:\Users\USER\AppData\Local\Temp\Discord Can ...

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

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

  8. 21 JSON and Go go语言和json

    JSON and Go 25 January 2011 Introduction JSON (JavaScript Object Notation) is a simple data intercha ...

  9. NVME SSD vs SATA SSD(转)

    NVMe是个啥?未来SSD主流标准早知 关注固态硬盘的朋友应该对于这个词汇并不陌生,特别是今年NVMe也频繁出现在各大媒体文章中,随着高端SSD市场逐渐从SATA专项PCI-E时,以前的AHCI标准已 ...

  10. Javascript 跨域访问解决方案 总结

    在客户端编程语言中,如javascript和ActionScript,同源策略是一个很重要的安全理念,它在保证数据的安全性方面有着重要的意义.同 源策略规定跨域之间的脚本是隔离的,一个域的脚本不能访问 ...