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 ...
随机推荐
- 剑指offer-连续子数组的最大和-数组-python
题目描述 例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止). 给一个数组,返回它的最大连续子序列的和 思路:动态规划 # -*- coding:u ...
- 2019-11-29-C#-标准性能测试高级用法
title author date CreateTime categories C# 标准性能测试高级用法 lindexi 2019-11-29 10:13:16 +0800 2018-07-08 0 ...
- DNS记录
转载于:https://www.cnblogs.com/sddai/p/5703394.html 类型 SOA NS A AAAA PTR CNAME MX --------------------- ...
- SQLServer Transaction Isolation Level
基本用法 -- Syntax for SQL Server and Azure SQL Database SET TRANSACTION ISOLATION LEVEL { READ UNCOMMIT ...
- JS 循环遍历 总结
一.循环遍历语句 for...in... (ES5) 语法:javascript for(keys in obj){} 适用:遍历对象 说明: 1.keys表示obj对象的每一个键值对的键(键名),所 ...
- 查看系统的DPI
#include <Windows.h> #include <iostream> int main() { SetProcessDpiAwarenessContext(DPI_ ...
- SQL语句 数据类型
6.1 Data Type 查看数据所占空间的两个函数: -- 查看所占字节数 select length('你好,世界') from dual; -- 查看所占字符数,即多少个字母,多少个汉字 se ...
- Windows 2012 R2 DataCenter服务器 重启之后,其他加域电脑无法访问域账户
需在域控服务器重启,服务Kerberos Key
- 【SaltStack官方版】—— STORING JOB RESULTS IN AN EXTERNAL SYSTEM
STORING JOB RESULTS IN AN EXTERNAL SYSTEM After a job executes, job results are returned to the Salt ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...