leetcode70 爬楼梯

题目

image.png

分析

斐波那契数列。

代码

class Solution {
public:
    int climbStairs(int n) {
        int first = 1, second = 2;
        if (n == 1){
            return first;
        }else if (n == 2){
            return second;
        }else{
            int res;
            for (int i = 3; i <= n; i++){
                res = first + second;
                first = second;
                second = res;
            }
            return res;
        }
    }
};

你可能感兴趣的:(leetcode70 爬楼梯)