leetcode-mid-dynamic programming-55. Jump Game
mycode 71.47%
思路:
既然要到达终点,那么俺就可以倒推,要想到达n,可以有以下情况
1)到达n-1,然后该位置最少可以走一步
2)到达n-2,然后该位置最少可以走两步
3)到达n-3,然后该位置最少可以走三步
。。。。
emmmm...本来想按照这个方法写个函数,发现跑不通。。。。我就干脆先把list倒过来,当下个数大于等于,他能到达上一个数,但是当这个数暂时不能到达时,我就需要给下一个数加一个step的要求啦
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
nums[:] = nums[::-1]
length = len(nums)
step = 1
for i in range(1,length):
if nums[i] >= step:
step = 1
continue
else:
step += 1
return step == 1
参考:
跟我的差不多,区别:我的目标--不断衡量距离,他的目标--不断改变目的地
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if not nums or len(nums)==1 : return True
des = len(nums) - 1
for i in range(len(nums)-2,-1,-1):
if nums[i] >= des - i:
des = i
return des == 0
leetcode-mid-dynamic programming-55. Jump Game的更多相关文章
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] questions conclusion_ Dynamic Programming
Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...
- [LeetCode] 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 (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 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][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- element-ui使用el-tabs组件的时候浏览器直接卡死的问题
遇到这个问题的原由是:本身自己项目的elementUI版本是2.0.11较低了,项目有个功能是自定义progress进度条颜色,无奈高版本的才有这个配置,所以就升级了elementUI,升级到了最高版 ...
- KNN-机器学习算法
''' Created on Sep 16, 2010 kNN: k Nearest Neighbors Input: inX: vector to compare to existing datas ...
- python 元类 MetaClass
type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello.py模块: class Hel ...
- Deadlock_synchromized-Java_se
class Test implements Runnable{ private boolean flag; Test(boolean flag) { this.flag = flag; } publi ...
- 简单了解 node net 模块
简单了解 node net 模块 文章记录了对net 模块的简单理解分析. net模块 简单使用 net.Server 类 net.Socket 类 总结 1.1 net模块 Node.js 的 Ne ...
- 【检测工具】keepalived安装及配置
一.keepalived安装 keepalived是一个检测服务器状态的脚本,在高可用机制上经常可以看到它的身影. 在Linux中安装keepalived: 1.在网上直接下载相应的压缩包,推荐链接 ...
- SpringBoot页面展示Thymeleaf
https://www.jianshu.com/p/a842e5b5012e 开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了.Spring ...
- Linux学习--第十二天--服务、ps、top、pstree、kill、&、jobs、fg、vmstat、dmesg、free、uptime、uname、crontab、ls
服务分类 linux服务分为rpm包默认安装的服务和源码包安装的服务. rpm包默认安装的服务分为独立的服务和基于xinetd服务. 查询已安装的服务 rpm包安装的服务 chkconfig --li ...
- mysql orderby 问题
开发写的sql select * from aaa where course_id=xx order by a,b 当a,b条件都一致时,默认应该以id排序,当数据条数大于1x条(17)时,结果变为 ...
- JavaScript双重排序
前言:正好这两天正在做一个功能,需要在前台进行排序展示,因为是动态的,后台排序不能搞定,只能咋前台通过JS来进行排序展示,所以我们用sort()来解决这个问题,sort不仅能给数组,对象,集合进行简单 ...