Leetcode: 70. Climbing Stairs
2016-05-01 12:05
645 查看
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?
读一遍题如果没发现是斐波拉切数列基本可疑抹脖子了
使用递归计算,给的N比较大的话内存爆了那是妥妥的。
就按照
递归
o(n) + o(1)
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
读一遍题如果没发现是斐波拉切数列基本可疑抹脖子了
使用递归计算,给的N比较大的话内存爆了那是妥妥的。
就按照
f(n) = f(n-1) + f(n-2)来计算。
递归
int fibolaci(int a,int b,int i,int n) { if(i>n) { return b; } return fibolaci(b,a+b,i+1,n); } int climbStairs(int n) { return fibolaci(0,1,1,n); }
o(n) + o(1)
int climbStairs(int n) { int pre = 1; int cur = 1; int step = 1; int temp ; while(step<n) { temp = cur; cur = cur + pre; pre = temp; step++; } return cur; }
相关文章推荐
- 【LeetCode】Contains Duplicate 解题报告
- 谈谈AI
- hdu 1166 敌兵布阵 + Dairy
- 程序定义了多个入口点。使用 /main (指定包含入口点的类型)进行编译
- 尾递归(Tail Recursion)和Continuation
- 最好用的Nema信号模拟器:NemaStudio 1.30.5885 开发者Nema 必备利器, 包括AIS Simulator,GPS Simulator,weather simulator,ra
- Summer Training Team Selection (1) Problem D Hidden Password STL,字符串匹配,简单题
- Summer Training Team Selection (1) Problem F Line Them Up 判断升序降序
- Summer Training Team Selection (1) Problem A ACM Contest Scoring 水题
- Title Four:Rainy
- TempDailyMonitor
- UVa 514 Rails
- MyBaits-快速入门
- Codeforces 667A Pouring Rain 667B Coat of Anticubism
- vc 6.0 中 error LNK2001: unresolved external symbol _main和fatal error LNK1120: 1 unresolved externals
- 免费邮件服务器:MailEnable
- LightOJ - 1236 Pairs Forming LCM 合数分解
- Codeforces Round #349 (Div. 2)-A. Pouring Rain(数学)
- ORA-01501: CREATE DATABASE failed
- Attended two wedding in the weekend