天天看點

【2019秋冬】【LeetCode】70 爬樓梯

class Solution {
public:
 
    int climbStairs(int n) {
        n += 1;
        return (pow((1 + sqrt(5)) / 2, n) - pow((1 - sqrt(5)) / 2, n)) / sqrt(5);
    }
};
           

爬樓梯(因為步數是1,2步,是以可以是前兩項之和),生兔子,都是斐波那契數列(目前項等于前兩項之和)

可以循環或者遞歸寫,或者數學公式,斐波那契數列求目前值公式

【2019秋冬】【LeetCode】70 爬樓梯