您的位置:首页 > 大数据 > 人工智能

【LeetCode】Climbing Stairs

2013-10-05 21:38 239 查看
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?

code : 老题目了,斐波那契数列,注意不用递归,记忆化或者前向递推复杂度是O(n).

class Solution {
public:
int climbStairs(int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n <= 1)
return 1;
int *fib = new int[n+1];
fib[0] = fib[1] = 1;
fib[2] = 2;
for(int i = 3; i <= n; i++)
{
fib[i] = fib[i-1] +fib[i-2];
}
int res = fib
;
delete [] fib;
fib = NULL;
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: