[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 ...
随机推荐
- 定位于定位优化(iOS)
定位于定位优化 1.定位的方案 最近在做项目, 遇到了一个问题就是如何让iOS7~~iOS9的定位.因为项目需求只是需要获取用户所在的城市, 而不用十分具体详细的精确定位, 服务端考虑用区号, 作为标 ...
- (转)mysql分表的3种方法
原文:http://blog.51yip.com/mysql/949.html 一,先说一下为什么要分表 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿 ...
- Python 获取Facebook用户Friends的爱好类别中的Top10
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...
- LDA-线性判别分析(一)
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- jsp当参数为空的时候默认显示值
当${business.branchName }为空或者不存在的时候显示“请选择门店” <c:out value="${business.branchName }" defa ...
- NET中级课--浅谈委托,事件,异步调用,回调等概念
直接说题. 委托 首先明确它是什么,其实就是一个类,定义一个委托即定义一个类,那么它是什么类?用来说明方法的类型的类.字段有类型,那么方法其实也有类型,就是委托. 委托是某 ...
- 关于document.write()重写页面
今天碰到了一个以前没注意的问题即:document.write(),在此拿来分享! document.write是最基本的JavaScript命令之一,这个命令简单地打印指定的文本内容到页面上(注意是 ...
- 怎么修改placeholder字体的css样式
修改palceholder内文字的css样式 ::-webkit-input-placeholder{ color: red; font-size: 20px; line-height: 50px; ...
- 黑马程序员—— Java SE(2)
----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训 ...
- ueditor富文本编辑的使用方法
平时在编写上传文件,图片,视频等等这些功能的代码会很繁琐,这里我介绍一款由百度推出的CuteEditor,是一款功能非常强大,支持图片上传.文件下载和word类似的文字编辑器.对于新闻发布系统和博客之 ...