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

[Leetcode 70]: Climbing Stairs

时间:2018-02-24 13:17:44      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:either   eps   bst   斐波那契   ali   tin   time   esc   本质   

Description: 

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?

Note: Given n will be a positive integer.

Analisis:

这个问题的本质其实就是斐波那契数列, 爬到第n级楼梯的步数,是由f(n-1)与f(n-2) 所控制的。

则: f(1)=1, f(2)=2, f(n)=f(n-1)+f(n-2);

 1 int climbStairs(int n) {
 2    
 3      int zr=1;
 4      int st=2;
 5      int pri=zr, per=st;
 6      for(int i=3; i<=n;i++)
 7      {
 8           if (i & 0x001 == 1)  //odd
 9                pri=pri+per;
10           else
11                per=pri+per;
12      }
13      if(n & 0x001 ==1)
14        return pri;
15      else
16        return per;
17     }

 

[Leetcode 70]: Climbing Stairs

标签:either   eps   bst   斐波那契   ali   tin   time   esc   本质   

原文地址:https://www.cnblogs.com/zhaoyaxing/p/8465022.html

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