[leetcode] climbing Stairs

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?

Fibonacci數列的題目,三種做法:

1. 遞歸

2. 循環

3. 利用Fibonacci數列通項公式

AC代碼就不貼了,簡單證明下通項公式:

遞推公式爲a[n+2]=a[n+1]+a[n],

其特徵方程爲x*x-x-1=0,它的兩個根即爲特徵根.即(1+√5)/2和(1-√5)/2,

爲表達方便,設它們爲A,B.則其通項公式爲a[n]=p*A^n+q*b^n,

其中p,q爲代定係數,通過a[0],a[1]的值可得p,q.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章