[编程题-京东]上台阶

[编程题] 上台阶

有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或者二级,要走上m级,共有多少走法?注:规定从一级到一级有0种走法。

给定一个正整数int n,请返回一个数,代表上楼的方式数。保证n小于等于100。为了防止溢出,请返回结果Mod 1000000007的值。

测试样例:
3
返回:2
class GoUpstairs {
public:
    int countWays(int n) {
        // write code here
        if(n==1) return 1;
        int f0=1;
        int f1=1;
        for(int i=3;i<=n;i++)
        {
            int tmp=f1;
            f1=(f1+f0)%(1000000007);
            f0=tmp;
        }
        return f1;
    }
};
原文地址:https://www.cnblogs.com/learning-c/p/5745839.html