[Leetcode 70]: Climbing Stairs

Posted zhaoyaxing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode 70]: Climbing Stairs相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 70. Climbing Stairs

Leetcode-70 Climbing Stairs

Leetcode 70 Climbing Stairs

[Leetcode 70]: Climbing Stairs

[leetcode]70.Climbing Stairs

LeetCode 70. Climbing Stairs