本文共 542 字,大约阅读时间需要 1 分钟。
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?
在 那个题目中,我们分析出完美情况下,f[i] = f [ i - 1] + f[ i - 2]。这里的情况恰好是完美情况。所以我们可以递归去求解,但n很大的时候递归那么长,有点不合适。
//code
class Solution {public: int climbStairs(int n) { if(n <= 1) return n; int f0 = 1; int f1 = 1; int f2; for(int i = 2; i <= n; ++i) { f2 = f1 + f0; f0 = f1; f1 = f2; } return f2; }};
转载地址:http://melji.baihongyu.com/