[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 ...
随机推荐
- [Oracle] 参数修改小结
v$parameter Oracle参数的修改比较复杂,有些参数是可以在session级别修改,有些则必须在system级别修改,有些参数修改后马上生效(不需要重启),有些参数则必须重启才能生效,那么 ...
- Linux网络编程--多播
一.多播介绍 什么是多播? 单播用于两个主机之间的端对端通信,广播用于一个主机对整个局域网上所有主机上的数据通信.单播和广播是两个极端,要么对一个主机进行通信,要么对整个局域网上的主机进行通信.实际情 ...
- hdu 5430 Reflect (数学推导题)
Problem Description We send a light from one point on a mirror material circle,it reflects N times a ...
- python高级编程之选择好名称:完
由于时间关系,python高级编程不在放在这边进行学习了,如果需要的朋友可以看下面的网盘进行下载 # # -*- coding: utf-8 -*- # # python:2.x # __author ...
- pyqt 托盘例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from PyQ ...
- 为项目编写Readme.MD文件
了解一个项目,恐怕首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme. ...
- Js 实现 C# Format方法
参考网友的, 挺好用的: String.prototype.format = function (args) { if (arguments.length > 0) { var result = ...
- FullCalendar 的学习笔记(一)
前一段时间,一个老项目需要新增一个小功能,日程表~ 于是网上找了下,发现FullCalendar这个控件还不错于是就拿来用了下,下面简单介绍下我的学习笔记. 首先就是了解下FullCalendar的A ...
- 提示框的优化之自定义Toast组件之(三)Toast组件优化
开发步骤: 在toast_customer.xml文件中添加一个图片组件对象显示提示图片 <?xml version="1.0" encoding="utf-8&q ...
- 真机调试以及“Could not find Developer Disk Image”问题解决方案
真机测试步骤 1.运行Xcode,Xcode打开后,点左上角菜单'Xcode',点'Preferences'. 2.在打开的窗口中,点'Accounts',切换到账号页,然后点下面的'+'号,在弹出菜 ...