码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-01-14 12:19:42      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:round   斐波那契   rgb   微软雅黑   www   color   cas   enum   title   


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. }





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

标签:round   斐波那契   rgb   微软雅黑   www   color   cas   enum   title   

原文地址:http://www.cnblogs.com/xiejunzhao/p/420b858048e643866608d6d60038b6f7.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!