[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 ...
随机推荐
- ZOJ3829---模拟,贪心
这是2014年ACM亚洲区预赛牡丹江现场赛的一道题,铜牌题,可惜当时一路WA到死... 只有乘法的后缀表达式的特点有两个:(1)数字的数量一定大于‘*’的数量(2)最后一位一定是‘*’: 数字比*多的 ...
- python json基础学习01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import json #全称(javascript object ...
- android自己定义ViewPager之——3D效果应用
今天在github里看到一个3D效果的ViewPager,感觉做出来的ViewPager滑动的时候效果十分的炫,就check out下来研究了一下怎样实现的.以及怎样使用.将整个ViewPager稍加 ...
- 同一个页面多个CALayer重绘的办法
//知识点,CALayer的重绘,-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 方法,CALayer的渐变色.多个CALa ...
- Hacker(八)----NET命令
NET命令是一种基于网络的命令,该命令的功能很强大,可以管理网络环境.服务.用户和登录等本地及远程信息.常见的NET命令主要有net view.net user.net use.net time.ne ...
- SQL语句中的DQL、DML、DCL、DDL、CCL、TPL
结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统:同时也是数据库 ...
- Android编译过程详解(三)
前面两节讲解了自定义Android编译项和创建Product产品配置文件,除了编译和定义产品相关环境变量外,还需要定义Board相关环境变量. 1. build/core/config.mk 109 ...
- EffectiveC#12,13,14--成员初始化
1.在一个类里声明变量的同时,直接创建实例值.包括静态的和实例的变量 例:object m_o = new object(); 如下情况时不建议这么做:第一种 值类型. int i=new int() ...
- Query获取值常用
Query获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...}); //为Sel ...
- css-盒模型
<!DOCTYPE html>CSS3-盒模型 盒模型属性: width 宽度 height 高度 margin 外边距 border 边框 padding 内边距.test{width: ...