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 ...
随机推荐
- 吴恩达深度学习:2.16关于python/numpy
- bat批处理的注释语句
写bat批处理也一样,都要用到注释的功能,这是为了程式的可读性 在批处理中,段注释有一种比较常用的方法: goto start = 可以是多行文本,可以是命令 = 可以包含重定向符 ...
- Delphi 赋值语句和程序的顺序结构
- ieda与svn的配置与使用
一.idea配置svn 快捷键Ctrl+Alt+s或者File--Settings-- Subversion 设置svn客户端(小乌龟)的svn.exe可执行程序(如果找不到,请看另一篇文章) ...
- svn 介绍及linux下常用操作命令
1.概念 truck(主干|主线|主分支):是用来做主方向开发的,新功能的开发应放在主线中,当模块开发完成后,需要修改,就用branch. branch(分支):分支开发和主线开发是可以同时进行的,也 ...
- 最简单的Android项目(添加jar文件)
如果项目需要引用第三方jar文件,需要对编译命令做一些改动. 首先在项目根目录创建libs目录,将需要的jar文件拷贝到里面. 编译过程中有两步需要改动. 编译java源文件时,需要添加class p ...
- Java并发编程实战 第11章 性能与可伸缩性
关于性能 性能的衡量标准有很多,如: 服务时间,等待时间用来衡量程序的"运行速度""多快". 吞吐量,生产量用于衡量程序的"处理能力",能够 ...
- Spring mvc 搭建Mybatis
本文建立在spring mvc已经搭建起来的基础上. 首先看要引入的jar包,其中高亮的是为了mybatis新引入的. <properties> <spring.webm ...
- webpack 热更新
1.安装webpack npm install webpack -g //全局安装 npm install webpack --save-dev //开发环境 2.使用webpack 创建一个we ...
- 甘特图 dhtmlx 插件
https://dhtmlx.com/docs/products/demoApps/advanced-gantt-chart/