# leetcode笔记：Climbing Stairs（斐波那契数列问题）

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?

? 从第`f(n-1)`阶前进`1`步；

? 从第`f(n-2)`阶前进`2`步；

`f(n)`可写成：`f(n) = f(n-1) + f(n-2)`

``````#include <iostream>
using namespace std;

class Solution
{
public:
// 时间复杂度O(1)
int climbStairs1(const int n)
{
const double sqrtNum = sqrt(5);
return int(floor((pow((1 + sqrtNum) / 2, n + 1) - pow((1 - sqrtNum) / 2, n + 1)) / sqrtNum));
}

// 时间复杂度O(n)
int climbStairs2(const int n)
{
int current = 1;
int last = 0;
for (int i = 1; i <= n; i++)
{
int temp = current;
current += last;
last = temp;
}
return current;
}
};``````

``````#include "ClimbingStairs.h"
#include <iostream>

int main()
{
int n;
cout << "How many stairs? " << "Input: ";
cin >> n;
Solution s;
int result1 = s.climbStairs1(n);
int result2 = s.climbStairs2(n);

cout << "How many ways to reach the finish line? " "Result1：" << result1 << endl;
cout << "How many ways to reach the finish line? " "Result2：" << result2 << endl;
system("pause");
return 0;
}``````

## [LeetCode] Climbing Stairs 斐波那契数列

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? Show Tags 这题其实就是斐波那契数列来的. #include <iostream> using namespace std; class Solution {

## LeetCode | 面试题10- I. 斐波那契数列【剑指Offer】【Python】

LeetCode 面试题10- I. 斐波那契数列[剑指Offer][Easy][Python][动态规划] 问题 力扣 写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项.斐波那契数列的定义如下: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出. 答案需要取模 1e9+7(1000000007),如计算初始结果为:10000000

## 70. 爬梯子问题（斐波那契数列）Climbing Stairs

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? 假设你在爬一个楼梯,该楼梯有n阶,你有两种爬法,每次爬一阶或者两阶.请问你有多少种爬法到达楼梯顶部. public int ClimbStairs(int n) { i

## 13、蛤蟆的数据结构笔记之十三栈的应用之栈与递归之斐波那契数列

13.蛤蟆的数据结构笔记之十三栈的应用之栈与递归之斐波那契数列 本篇名言:"人生不是一支短短的蜡烛,而是一支由我们暂时拿着的火炬,我们一定要把它燃得." 继续递归的斐波那契数列问题. 欢迎转载,转载请标明出处: 1.  斐波那契数列 斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.--在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)在现代物理.准晶体结构.化学