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 ...
随机推荐
- Oracle、DB2、MySql、SQLServer JDBC驱动
四种数据库JDBC驱动,还列出了连接的Class驱动名和Url Pattern,DB2包括Type 2.Type 3和Type 4三种模式.注意驱动包名称的大小写. Oralce连接驱动包名和URL ...
- KMP算法 KMP模式匹配 一(串)
A - KMP模式匹配 一(串) Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:131072KB ...
- AVL树的插入删除查找算法实现和分析-1
至于什么是AVL树和AVL树的一些概念问题在这里就不多说了,下面是我写的代码,里面的注释非常详细地说明了实现的思想和方法. 因为在操作时真正需要的是子树高度的差,所以这里采用-1,0,1来表示左子树和 ...
- C# webBrowser操作 javascript
using System; using System.Windows.Forms; namespace Demo { public partial class Form1 : Form { publi ...
- tar打包过滤某个文件及文件夹
ip=ip add|grep eth0|grep -i inet|awk '{print $2}'|cut -d '/' -f 1 cd /data tar -zvcf `echo $ip`_`dat ...
- Boost Thread学习笔记五
多线程编程中还有一个重要的概念:Thread Local Store(TLS,线程局部存储),在boost中,TLS也被称作TSS,Thread Specific Storage.boost::thr ...
- 在Mac OS X苹果lion系统上制作USB启动盘
本文翻译自:http://evan.borgstrom.ca/post/1314205955/osx-bootable-usb-from-iso 我也就不按照原文上一句句的翻译了,只说几个比较重要的步 ...
- 流式计算-Jstorm提交Topology过程(上)
Topology是Jstorm对有向无环图的抽象,内部封装了数据来源spout和数据处理单元bolt,以及spout和bolt.bolt和bolt之间的关系.它能够被提交到Jstorm集群. 本文以J ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- Ubuntu 安装和配置minicom
Ubuntu 安装和配置minicom 1 . 安装 Minicom 用新立得软件管理器下载minicom 2.配置Minicom shell下输入 minicom -s 打开配置界面 进入Seria ...