LeetCode 70. Climbing Stairs

来源:互联网 发布:手机短信轰炸软件下载 编辑:程序博客网 时间:2024/05/16 01:16

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.

程序代码:

int climbStairs(int n) {    int TwoStep=1,OneStep=1,i,sum=0;    if(n==1)        sum = 1;    for(i=2;i<=n;i++)    {        sum = TwoStep+OneStep;        TwoStep = OneStep;        OneStep= sum;    }    return sum;}
0 0