[leetcode 70]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?
爬臺階,你每次能夠上一層或者兩層,問上n層你共同擁有多少何種方法
f(n)=f(n-1)+f(n-2)
動規,記得保存n-1和n-2的結果,直接使用遞歸會超時
AC代碼
class Solution {
public:
? ? int climbStairs(int n) {
? ? int count[n+1];
? ? count[0]=0;
? ? count[1]=1;
? ? count[2]=2;
? ? if(n==1)
? ? ? ? return 1;
? ? if(n==2)
? ? ? ? return 2;
? ? for(int i=3;i<=n;++i)
? ? ? ? count[i]=count[i-1]+count[i-2];
? ? return count[n];
? ? }
};
其它Leetcode題目AC代碼:https://github.com/PoughER/leetcode
轉載于:https://www.cnblogs.com/brucemengbm/p/6915696.html
總結
以上是生活随笔為你收集整理的[leetcode 70]Climbing Stairs的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF 程序加载PGIS性能问题
- 下一篇: 模拟电路——反馈