Leetcode_num13_Climbing Stairs
称号:
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
非常easy的思路。由于一次能够走1~2步,所以到达第n级能够从第n-1级,也能够从第n-2级。
设到达第n级的方法有s(n)种,s(n)=s(n-1)+s(n-2)
一開始准备用递归做。代码例如以下:
class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n<=2:
return n
else:
return self.climbStairs(n-1)+self.climbStairs(n-2)
结果在n=35的时候TLE了。这进一步说明递归的算法效率比較低,但从思路上比較简单明了。
于是,转向迭代了,代码例如以下:
class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n<=1:
return n
else:
s=[0 for i in range(n)]
s[0]=1 #到达第1级
s[1]=2 #到达第2级
for i in range(2,n):
s[i]=s[i-1]+s[i-2]
return s[n-1] #到达第n级
在此引入一个数组s,记录到达第n级的方法,然实际要求的返回值是s[n],数组s中的前n-1项存储值是多余的。
于是进行改进。设s1为走一步到达方法数。s2为走两步到达的方法数。那么到达第n级台阶时,s(n)=s1+s2,当中s1=s(n-1),s2=s(n-2);到达第n+1级台阶时。s(n+1)=s1+s2,当中s1=s(n)=上一步的s1+s2, s2=s(n-1)=上一步的s1,所以仅仅须要记录s1和s2的值。无需记录n个值
class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n<=1:
return n
else:
s1=1
s2=1
for i in range(1,n):
s=s1+s2
s2=s1
s1=s
return s
这应该是比較简单的方法了。受教了。
版权声明:本文博客原创文章。博客,未经同意,不得转载。
Leetcode_num13_Climbing Stairs的更多相关文章
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- LintCode Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- Cllimbing Stairs [LeetCode 70]
1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- Scala中Stream的应用场景及事实上现原理
欢迎訪问我的独立博客:http://cuipengfei.me/blog/2014/10/23/scala-stream-application-scenario-and-how-its-implem ...
- 安装windows7导致Ubuntu启动项消失的问题的解决
系统原来是Ubuntu14,前两天安装win7后,启动直接是win7.也就是Ubuntu的启动项消失了. 在windows下尝试非常多方法,都以失败告终,最后选择Ubuntu下boot-repair软 ...
- JQuery - 去除所有空格
$('#submit').click(function () { //去除所有空格 String.prototype.NoSpace = function () { return this.repla ...
- 初入Android--Activate生命周期
Activate的主要生命周期 (注意:这只是主要的生命周期,而不是完整的生命周期方法,其中的两个周期之间可能还执行了其他的一些方法) 每个时刻在屏幕上的状态 进入onCreate方法:Activat ...
- Qt 多线程 详细函数说明及其事例
转:http://www.cnblogs.com/hicjiajia/archive/2011/02/03/1948955.html Qt线程类 Qt 包含下面一些线程相关的类:QThread 提供了 ...
- 强化一下开源库:Synopse
http://synopse.info/fossil/wiki/Synopse+OpenSource 有空要研究一下,只有写在这里,才会时时刻刻提醒自己.
- GreenDAO数据库版本升级
GreenDAO是一款非要流行的android平台上的数据库框架,性能优秀,代码简洁. 初始化数据库模型代码的时候需要使用java项目生成代码,依赖的jar包已经上传到我的资源里了,下载地址如下:ht ...
- 解决cocoapods在64位iOS7系统以下的警告问题
今天碰到一个非常奇怪的问题.XCODE提示这种警告 Pods was rejected as an implicit dependency for 'libPods.a' because its ar ...
- Oracle性能分析7:创建索引
在创建索引时,我们往往希望可以预估索引大小,以评估对现有project环境的影响,我们也希望创建索引的过程可以最小化的影响我们正在执行的project环境,并能查看索引的状况. 预估索引大小 预估索引 ...
- 14.9 InnoDB Row Storage and Row Formats InnoDB 行存储和行格式:
14.9 InnoDB Row Storage and Row Formats InnoDB 行存储和行格式: 14.9.1 Overview of InnoDB Row Storage 14.9.2 ...