[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 {
public:
    int climbStairs(int n) {
        if(n<2) return 1;
        int l=1,r=1,tmp;
        for(int i=2;i<=n;i++){
            tmp=l+r;
            l=r;
            r=tmp;
        }
        return r;
    }
};

int main()
{
    Solution sol;
    for(int i =0;i<9;i++)
    cout<<sol.climbStairs(i)<<endl;
    return 0;
}

时间: 2024-08-25 10:09:04

[LeetCode] Climbing Stairs 斐波那契数列的相关文章

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? 解析:设直至第n层有f(n)中走法,因为每次只能走1步或2步,则 f(n) = f(n-1) + f(n-2) 很明显是一个斐波那契数,f(0) = 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? 题目的大意是,已知有n阶楼梯,每次只能爬1阶或2阶楼梯,问爬到第n阶楼梯共有几种爬法-_-||.题目可以看成是,设f(n)表示爬到第n 阶楼梯的方法数,为

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

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

斐波那契数列实例讲解以及C++实现

斐波那契数列,又称黄金分割数列,指的是这样一个数列: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*)在现代物理.准晶体结构.化学等领域,斐波纳契数列都有直接的应用,为此,美国数学会从1963起出版了以<斐波纳契数列季刊>为名的一份数学杂志,用于专门刊载这方面的研究成果. 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

对斐波那契数列的理解

在数学上,费波那契数列是以递归的方法来定义: (n≧2) 费波那契数列由0和1开始,之后的费波那契系数就是由之前的两数相加而得出. 与斐波那契数列有关的问题,都符合这样的描述: 当前状态的得出是依赖于前两项的状态,给出初始状态F(0),F(1),之后的每一项都满足共同的特点,即为前两项状态相加. 前两项的状态,分别为当前状态的两种解法,适用加法原理. 下面给出几个例题: 1.Climbing Stairs 爬楼梯问题 每次只能爬1或2步,那么爬到第n层的方法要么是从第n-1层一步上来的,要不就是

斐波那契数列 Library

http://acm.tju.edu.cn/toj/showp3267.html3267.   Library Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 214   Accepted Runs: 96 Description As we all know, there are very long stairs before our library in campus III of our school. It is so

用递归和非递归的方法输出斐波那契数列的第n个元素(C语言实现)

费波那契数列(意大利语:Successione di Fibonacci),又译为费波拿契数.斐波那契数列.费氏数列.黄金分割数列. 在数学上,费波那契数列是以递归的方法来定义: {\displaystyle F_{0}=0} {\displaystyle F_{1}=1} {\displaystyle F_{n}=F_{n-1}+F_{n-2}}(n≧2) 用文字来说,就是费波那契数列由0和1开始,之后的费波那契系数就是由之前的两数相加而得出.首几个费波那契系数是: 0, 1, 1, 2, 3

Fibonacci斐波拉契数列----------动态规划DP

n==10 20 30 40 50 46 体验一下,感受一下,运行时间 #include <stdio.h>int fib(int n){ if (n<=1)     return 1; else            return fib(n-1)+fib(n-2); }int main( ){ int n; scanf("%d",&n); printf("%d\n" ,fib(n) );} 先 n==10 20 30 40 50 46