Easy 17 Climbing Stairs(70)

来源:互联网 发布:冰点文库下载器mac版 编辑:程序博客网 时间:2024/05/23 11:54

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?

Solution
简单的动态规划的题目,dp[n]=dp[n-1]+dp[n-2],也满足斐波那契数列fibonacci。

class Solution {public:    int climbStairs(int n) {        if(n==1) return 1;        int a=1,b=2,res=2;        while(n-->2){            res=a+b;            a=b;            b=res;        }        return res;    }};
0 0
原创粉丝点击