[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 ...
随机推荐
- 【leetcode 简单】 第一百一十一题 可怜的小猪
有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...
- Dynamic Rankings(动态第k大+树套树)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题目: 思路: 树套树板子题. 代码实现如下: #inclu ...
- redis写定时任务获取root权限
前提: 1.redis由root用户启动. 2.开启cron的时候,/var/spool/cron linux机器下默认的计划任务,linux会定时去执行里面的任务. 启动服务 :/sbin/serv ...
- 倍增 Tarjan 求LCA
...
- CSV转excel方法
步骤一:新建excel文件,数据—>自文本,导入文件 步骤二:选择分隔符,下一步 步骤三:勾选分隔符符合,下一步 步骤四:直接下一步,可在预览里看到格式 步骤五:点击确定,等待数据导入
- Mysql查看建表语句以及修改引擎
更多内容推荐微信公众号,欢迎关注: 1 查看系统支持的存储引擎 show engines; 2 查看表使用的存储引擎 两种方法: a.show table status from db_name wh ...
- zookeeper zkClient api 使用
操作步骤: 一.引入zkclient的jar包(maven方式) <dependency> <groupId>com.101tec</groupId> <ar ...
- 18 - csv文件-ini文件处理
目录 1 CSV文件 1.1 手动生成一个csv文件 1.2 cvs模块 1.2.1 reader方法 1.2.2 writer方法 2 ini文件处理 2.1 configparser模块 2.2 ...
- a标签、img图片、iframe、表单元素、div
1.<a href="http://www.baidu.com" target=''_blank">百度</a> 超链接标签 2.<img ...
- c++ 类的构造顺序
在单继承的情况下,父类构造先于子类,子类析构先于父类,例: class A { public: A() { cout << "A" << endl; } ~ ...