LeetCode 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?

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;
}
全部评论

相关推荐

totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
10-30 10:16
南京大学 Java
龚至诚:给南大✌️跪了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务