https://leetcode.com/problems/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?

1)递归:  超时

class Solution {
public:
int climbStairs(int n) {
if (n==)
return ;
if(n==)
return ;
return climbStairs(n-)+climbStairs(n-);
}
};

2)非递归, 避免嵌套调用

递推公式

a_n=a_{n-1}+a_{n-2}.

a_1=1, a_2=2.

问题:给定n求a_n。其实就是斐波那契数列。

AC代码:

 class Solution {
public:
int climbStairs(int n) {
if (n==)
return ;
if (n==)
return ;
int t=,x=,y=,z;
while(t!=n){
t++;
z=x+y;
x=y;
y=z;
}
return z;
}
};

LeetCode(70)题解: climbing-stairs的更多相关文章

  1. [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  2. 【LeetCode练习题】Climbing Stairs

    Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...

  3. LeetCode(70) Climbing Stairs

    题目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli ...

  4. LeetCode算法题-Climbing Stairs(Java实现)

    这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...

  5. 【LeetCode】070. Climbing Stairs

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  6. Leetcode 题目整理 climbing stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. LeetCode OJ:Climbing Stairs(攀爬台阶)

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. Python3解leetcode Min Cost Climbing Stairs

    问题描述: On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you ...

  9. [LeetCode] 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. Oracle 的安装与使用

    一.文件下载 安装的是Oracle 11G,安装文件名为OracleXE112_Win32.zip, 官方文件下载地址:http://www.oracle.com/technetwork/databa ...

  2. 【CF173B】Chamber of Secrets(二分图,最短路)

    题意:给你一个n*m的地图,现在有一束激光从左上角往右边射出,每遇到‘#’,你可以选择光线往四个方向射出,或者什么都不做,问最少需要多少个‘#’往四个方向射出才能使关系在n行往右边射出. 思路:将每一 ...

  3. one day php. alomost all;

    <? namespace Test; use \PhpProject\PhpApp as Other; $u=new Other("ns test"); echo $u-&g ...

  4. MySQL的@与@@区别

    MySQL的@与@@区别 @x 是 用户自定义的变量  (User variables are written as @var_name) @@x 是 global或session变量  (@@glo ...

  5. python笔记1:python基础

    1.Python模块的标准文件模板: #!/usr/bin/env python #第1行注释可以让这个 .py 文件直接在Unix/Linux/Mac上运行 # -*- coding: utf-8 ...

  6. SpringBoot+Mybatis增删改查实战

    简介 SpringBoot和Mybatis是啥请自行百度,作者这里也是花了几天时间入门了这个框架用来完成任务,并且也算符合要求的完成了任务,期间也各种百度但是没找到自己想要的那种简单易懂的教程,所以踩 ...

  7. Codeforces A. Bear and Big Brother

    ...不行.这题之后.不做1000分以下的了.很耻辱   A. Bear and Big Brother time limit per test 1 second memory limit per t ...

  8. JavaSwing仿QQ登录界面,注释完善,适合新手学习

    使用说明: 这是一个java做的仿制QQ登录界面,界面仅使用一个类, JDK版本为jdk-11 素材包的名字为:素材(下载)请在项目中新建一个名字为“素材”的文件夹. 素材: https://pan. ...

  9. kafka技术分享01--------why we study kafka?

    kafka技术分享01--------why we study kafka? ​ 作为一名大数据工程师,我们所面对的大多数是数据密集型的应用,而非计算密集型的应用.对于数据密集型的应用,如何解决数据激 ...

  10. 词典对象(NSDictionary和NSMutableDictionary)

    词典(dictionary)顾名思义就是同由键-值组成的数据集合.与在词典中查找单词定义一样,可以通过对象的键从词典中获取需要的对象,看到 这里,你是不是想起了java中的map?和NSArray一样 ...