70. 爬梯子问题(斐波那契数列)Climbing Stairs
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
以上是关于70. 爬梯子问题(斐波那契数列)Climbing Stairs的主要内容,如果未能解决你的问题,请参考以下文章