首页 > 代码库 > 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阶,你有两种爬法,每次爬一阶或者两阶。请问你有多少种爬法到达楼梯顶部。


  1. public int ClimbStairs(int n) {
  2. if(n == 0)
  3. return 0;
  4. int pre = 0;
  5. int cur = 1;
  6. int tempCur = 0;
  7. for (int i = 1; i <= n; i++)
  8. {
  9. tempCur = cur;
  10. cur = pre + cur;
  11. pre = tempCur;
  12. }
  13. return cur;
  14. }


求斐波那契数列
  1. static public int[] Fibonacci(int n)
  2. {
  3. int[] arr = new int[n+1];
  4. arr[0] = 0;
  5. arr[1] = 1;
  6. for (int i = 2; i < n + 1; i++)
  7. {
  8. arr[i] = arr[i - 1] + arr[i - 2];
  9. }
  10. return arr;
  11. }



来自为知笔记(Wiz)


70. 爬梯子问题(斐波那契数列)Climbing Stairs