Climbing Stairs
来源:互联网 发布:美国近期非农数据预测 编辑:程序博客网 时间:2024/05/29 09:50
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?
分析:
题目比较简单,小的细节没有把握好。
class Solution {public: int climbStairs(int n) { if(n==1) return 1; int all[n+1]; all[0] = 1; all[1] = 1; for(int i=2;i<=n;i++){ all[i] = all[i-1] + all[i-2]; } return all[n]; }};
class Solution: # @param n, an integer # @return an integer def climbStairs(self, n): if n==1: return 1 all = [0]*(n+1) all[1] = 1 all[2] = 2 for i in range(3,n+1): all[i] = all[i-1] + all[i-2] return all[n]
总结:
1. 又忘了判断n, 注意任何一个输入的变量都要先想一想
2. 这是fabonacci 数列,其实只要保存两个值就可以了。 因为 fabonacci 数列有通项公式,所有可以用 log(n+1) 的复杂度解出来
0 0
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Climbing Stairs
- Android MaoZhuaWeiBo 好友动态信息列表数据抓取 -3
- Linkedln技术高管Jay Kreps:Lambda架构剖析
- Lambda架构
- Slide switch of div
- Android MaoZhuaWeiBo Adapter ListView优化及发送微博 - 4
- Climbing Stairs
- 程序员每年要做的十件事
- HTML5 textarea placeholder not appearing
- JavaScript function scope between script tags
- ASP.NET中向JS传递参数
- Can I use multiple versions of jQuery on the same page?
- Minimum Path Sum
- Git: Delete a branch (local or remote)
- jQuery to disable/enable textarea