[Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 55: Jump Game
https://leetcode.com/problems/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. === Comments by Dabay===
每次计算能到达的新范围。
如果新的范围超越了len(A)-1就范围True.
''' class Solution:
# @param A, a list of integers
# @return a boolean
def canJump(self, A):
if len(A) <= 1:
return True
start = 0
end = start + A[start]
while end >= start:
if end >= len(A) - 1:
return True
new_end = end
for i in xrange(start, end+1):
new_end = max(new_end, i + A[i])
start, end = end+1, new_end
else:
return False def main():
sol = Solution()
A = [1,1,1,0]
print sol.canJump(A) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]55: Jump Game的更多相关文章
- 【一天一道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][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- 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]55. Jump Game
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 ...
- 【LeetCode】45. Jump Game II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- 55. Jump Game leetcode
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
随机推荐
- sudo nopasswd
preface,不问头条,但汝读荐,诚意满满的!
- Direct3D 顶点缓存
今天我们来学习下Direct3D的顶点和顶点缓存,首先我们需要在场景中绘制一些物体,物体都是由多个三角形组成,每一个三角形由三个顶点组成,我们来看下面一个NPC的模型 左图:正常的模型 ...
- 安卓查询当前所在地天气及查询地区(城市)代码cityCode localCode
源码可获取用户当前位置的天气情况 本代码最有价值的部分在于关于城市码的获取,我用了两个小时才将全国主要城市的编码整理成HashMap,下载即可用! 试一试:点击下载. ---------------- ...
- 显示器 RUIGE瑞鸽高清显示器TL-S1700HD
版权声明:本文博客琅邪工作室原创文章,博客,未经同意不得转载.
- Qt之操作Excel
Visual Basic for Applications(VBA)是一种Visual Basic的一种宏语言,主要能用来扩展Windows的应用程式功能,特别是Microsoft Office软件. ...
- ARM9嵌入式学习笔记(1)-Linux命令
ARM9嵌入式学习笔记(1)-Linux命令 实验1-1-2 Linux常见命令使用 添加用户useradd smb; 设置账户密码passwd smb; 切换用户su - root 关机命令shut ...
- .net 地址栏传中文乱码 的解决方法
1.设置web.config文件. <system.web> ...... <globalization requestEncoding="gb2312" res ...
- JavaScript match 和 exec 备忘笔记
这是一道广为引用的面试题:var someText="web2.0 .net2.0"; var pattern=/(\w+)(\d)\.(\d)/g; var outCome_ex ...
- javascript 实现禁止右键,复制,选取文本 (兼容firefox,IE,chrome等主流浏览器)
1. JS 禁止右键 <script type="text/javascript">document.oncontextmenu=function(e){return ...
- 那些我希望在一开始使用 Zsh(oh-my-zsh) 时就知道的
原文地址:http://segmentfault.com/a/1190000002658335 作者:xavier 自带的插件 其实我用了 oh-my-zsh 快三个月后才知道原来他自带了很多插件没 ...