首页 > 代码库 > 70. 爬梯子问题(斐波那契数列)Climbing Stairs
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阶,你有两种爬法,每次爬一阶或者两阶。请问你有多少种爬法到达楼梯顶部。
public int ClimbStairs(int n) {
if(n == 0)
return 0;
int pre = 0;
int cur = 1;
int tempCur = 0;
for (int i = 1; i <= n; i++)
{
tempCur = cur;
cur = pre + cur;
pre = tempCur;
}
return cur;
}
求斐波那契数列
static public int[] Fibonacci(int n)
{
int[] arr = new int[n+1];
arr[0] = 0;
arr[1] = 1;
for (int i = 2; i < n + 1; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
return arr;
}
来自为知笔记(Wiz)
70. 爬梯子问题(斐波那契数列)Climbing Stairs
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。