LeetCode#70 Climbing Stairs
2015-07-23 19:25
344 查看
Problem Definition:
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?
Solution: 可以用动态规划来解。令爬n级阶梯的方式有f(n)种。
要到达第n级阶梯,要么是从第n-1级阶梯爬一步,要么是从第n-2级阶梯爬两步(一次性)。而这两种情况是没有交集的。
因此得到:f(n)=f(n-1)+f(n-2) (当n>1)
oops...正是斐波那契数列。
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?
Solution: 可以用动态规划来解。令爬n级阶梯的方式有f(n)种。
要到达第n级阶梯,要么是从第n-1级阶梯爬一步,要么是从第n-2级阶梯爬两步(一次性)。而这两种情况是没有交集的。
因此得到:f(n)=f(n-1)+f(n-2) (当n>1)
oops...正是斐波那契数列。
def climbStairs(n): a,b,i=[1]*3 while i<n: a,b=b,a+b i+=1 return b
相关文章推荐
- 2015 Multi-University Training Contest 2 1004 苹果树 dp+单调队列
- 2015多校联合训练赛 hdu 5308 I Wanna Become A 24-Point Master 2015 Multi-University Training Contest 2 构造题
- 2015 Multi-University Training Contest 2 1006 Friends
- 理解OpenContrail(by quqi99)
- Lua中闭包详解 来自RingOfTheC[ring.of.the.c@gmail.com]
- activity_main.xml
- 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- error Link2005:"pulbic:_thiscall class:run()已经在main.class。obj中定义
- Rails 上传图片
- AIDL(不完整)
- CLOSE_WAIT TIME_WAIT
- Failure [INSTALL_FAILED_OLDER_SDK]解决办法
- Failure [INSTALL_FAILED_OLDER_SDK]解决办法
- lightoj 1138 - Trailing Zeroes (III)【二分】
- OC笔记-学习self时候2个target然后修改名字出现linker command failed with exit code 1 (use -v to see invocation)
- Configuring HDFS High Availability
- “FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”。如果是有意隐藏,请使用关键字 new。
- AIDL的in out inout
- 安装inotify-tools,用inotifywait命令监听文件或目录的访问信息
- WEKA “Detailed Accuracy By Class”和“Confusion Matrix”含义