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 ...
随机推荐
- solve update pip 10.0.0
The bug is found in pip 10.0.0. In linux you need to modify file: /usr/bin/pip from: from pip import ...
- 097、如何实现Service伸缩?(Swarm04)
参考https://www.cnblogs.com/CloudMan6/p/7885667.html 上一节部署了只有一个副本的Service,不过对于web服务,我们通常会运行多个实例,这样可以 ...
- java json对象转换
引入的jar包: commons-beanutils-1.9.2.jar commons-collections-3.2.1.jar commons-lang-2.6.jar commons-logg ...
- js将时间戳转化为年月日时分秒
export const dateFormatter = (nows) => { if (!nows) return '' var now = new Date(nows) var year = ...
- python编码环境安装与基本语法
一.pycharm的基本使用 1.python以及pycharm的安装 python的版本选择:3.x版本就行 pycharm的版本选择:社区版就够用 pycharm只是一个编写工具,python才是 ...
- 一、移动端商城 Vue 组件库
一.组件库 移动端商城 Vue 组件库
- AIX中的页空间管理
1.页空间简介(Paging Space) 页空间是指硬盘上的存储内存信息的区域. 一个页空间也叫做一个交换空间. 是系统中一个类型为paging的逻辑卷. 2.创建页空间 使用mkps ...
- 负载均衡实现,一个域名对应多个IP地址【转载】
使用负载均衡实现,传统和常规做法,其他方式需要特殊处理.(dns轮询,或者自己做解析)1.一个域名设定多个dns服务或者服务器进行解析,同一个域名的每个解析都指向不同的ip地址,这样应答快的dns优先 ...
- 【学习】027 Dubbo
Dubbo概述 Dubbo的背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 ...
- 什么是lease机制?
分布式系统理论之租约机制学习 一,租约机制介绍 在分布式系统中,往往会有一个中心服务器节点.该节点负责存储.维护系统中的元数据.如果系统中的各种操作都依赖于中心服务器上的元数据,那么中心服务器很容易成 ...