# Leetcode notes: Climbing Stairs (Fibonacci sequence problem)

Source: Internet
Author: User
Tags pow

I. Description of the topic

You are are climbing a stair case. It takes n steps to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can your climb to the top?

The main idea is, known to have N-step staircase, can only climb 1-step or 2-step staircase, asked to climb to the nth staircase a total of several climbing method-_-| | The topic can be seen as, let F (n) indicate the number of methods to climb to the nth staircase, in order to climb to the nth staircase, there are the following two options:
• 1 steps forward from the F (n-1) step;
• 2 steps forward from the F (n-2) step;
then f (n) can be written as: F (n) = f (n-1) + f (n-2)

To the question of the Fibonacci sequence, there is a lot of online research on this content, the concept of Portal:
Http://baike.baidu.com/link?url=c2Bmk2jBGbI46qTIA-qKmdTkYBrVYYrejAHzf8BJRwCekIL4Sbx48fFCRkeGdul0

two. Topic Analysis

With respect to the Fibonacci sequence, you can use recursion or iteration to solve the problem, which can be written in the following recursive relationships:

It is obvious that iterative iteration is not the optimal solution, and when the value of f (n) is computed, all values of f (1), F (2),..., F (n-1) need to be computed, and there are many repetitive operations, such as the calculation of F (4) =f (3) +f (2), which needs to solve F (3) =f (2) +f (1). If you use an array to store all the calculated items, you can reduce the time complexity to O (n), and the space complexity is O (n).

In order to pursue the complexity of time, the direct use of Fibonacci formula, the derivation of the formula is as follows:

three. Sample Code

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

Class Solution
{public
:
//Time complexity 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);

Time complexity 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;
}
;```

Simple test Code:

```#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 the finish line?" "RESULT1:" << result1 << Endl;
cout << "How many ways the finish line?" "RESULT2:" << result2 << Endl;
System ("pause");
return 0;
}```

Four. Summary

In fact, there is also a loophole in the use of the formula, because the formula uses floating-point operations, there are also physical books, and therefore can not guarantee the accuracy of the results. In the book "The Beauty of programming", we also give a solution to the strategy of divide and conquer. The algorithm can achieve the time complexity O (log2n), and online More posts have written seven kinds of Jiefepo of the method, but also to continue in-depth research AH.

Related Keywords:

### Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.