LeetCode 20200213(爬楼梯)

1.爬楼梯
不知道为什么编译不通过
但是很简单就是斐波那契数列的思路
跳n个台阶 每次可以1 or 2
那么我跳n个的方法数就等于 跳n-1 个的方法(再跳一个)加上 跳 n-2 个的方法(再跳2个) 没其他的了 这儿就是斐波那契的方法

下面是我自己的方法 看一下问题出在哪儿
(编译未通过)

class Solution {
public:
    int climbStairs(int n) {
        return res(n);
    }
    int res(int n){
        if(n==1){return 1;}
        if(n==2){return 2;}
        if(n>2)return res(n-1)+res(n-2);
        }
    
};
发布了60 篇原创文章 · 获赞 9 · 访问量 3960

猜你喜欢

转载自blog.csdn.net/puying1/article/details/104333066