[leetcode greedy]55. Jump Game
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的更多相关文章
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- 【LeetCode】55. Jump Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- LeetCode OJ 55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode greedy]45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】55. Jump Game
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 55. Jump Game leetcode
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
- 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 ...
随机推荐
- 基本控件文档-UILabel属性
CHENYILONG Blog 基本控件文档-UILabel属性 Fullscreen UILabel属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...
- 基本控件文档-UISegment属性
CHENYILONG Blog 基本控件文档-UISegment属性 Fullscreen UISegment属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪 ...
- 29、HashSet简介
Set的特点 Set里面存储的元素不能重复,没有索引,存取顺序不一致. package com.monkey1024.set; import java.util.HashSet; /** * Set的 ...
- 创造101:如果软件测试工程师组团出道,怎样才能站C位?!
作者 C位出道的华华 虽然华华是一个软件测试技术宅,可以连续七七四十九天加班不重样,心里除了上班工作学习写代码就没有别的爱好了,但是各种潮流资讯啊狗血剧啊娱乐综艺啊,从来没有错过. 比如说现在大火的& ...
- 差分约束系统 + spfa(A - Layout POJ - 3169)
题目链接:https://cn.vjudge.net/contest/276233#problem/A 差分约束系统,假设当前有三个不等式 x- y <=t1 y-z<=t2 x-z< ...
- KVM,QEMU,libvirt入门学习笔记【转】
转自:http://blog.csdn.net/julykobe/article/details/27571387 注:本文内容均来自网络,我只是在此做了一些摘抄和整理的工作,来源均有注明. 0.虚拟 ...
- mipi 调试经验【转】
转自:http://blog.csdn.net/g_salamander/article/details/9163455 版权声明:本文为博主原创文章,未经博主允许不得转载. 以下是最近几个月在调试 ...
- Python Challenge 第 4 关攻略:linkedlist
代码 import requests url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing={}" ...
- juery下拉刷新,ajax请求,div加载更多元素(一)
;//设置当前页数 var flag=true; //滑动加载 $(function(){ var winH = $(window).height(); //页面可视区域高度 $(window).sc ...
- sed实例收集
url:http://blog.csdn.net/hepeng597/article/details/7852468 一.元字符集 1)^锚定行的开始 如:/^sed/匹配所有以sed开头的行. ...