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. 【BZOJ1001】狼抓兔子(平面图最小割转最短路)

    题意:有一张平面图,求它的最小割.N,M.表示网格的大小,N,M均小于等于1000. 左上角点为(1,1),右下角点为(N,M).有以下三种类型的道路  1:(x,y)<==>(x+1,y ...

  2. 头条PC端的鼠标经过图片放大效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. JAVA特性面试题:

    1.简要介绍java程序的健壮性. 答:JAVA程序会在编译和运行的时候自动的检测可能出现的错误,而且它是一种强类型语言,对于类型的检查很严格,而且它的垃圾回收机制也有效的避免了内存的泄漏. 2.为什 ...

  4. SPI设备的驱动

    主要包括两个SPI设备步骤:register_chrdevspi_register_driver关键点1:spi_board_info可以去已经运行的板子下面找例子:/sys/bus/spi/driv ...

  5. TStringList 善用 value['names'] 即使value 是带=号的值都没有关系呵呵 ,我靠 强,以后就用这个了,key=value首选

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA4YAAAHiCAIAAAA760U/AAAgAElEQVR4nOy9Z5QUV57oWe/j7tk9u2 ...

  6. npm-debug.log文件出现原因

    项目主目录下总是会出现这个文件,而且不止一个,原因是npm i 的时候,如果报错,就会增加一个此文件来显示报错信息,npm install的时候则不会出现.

  7. Codeforces 629 A. Far Relative’s Birthday Cake

      A. Far Relative’s Birthday Cake   time limit per test 1 second memory limit per test 256 megabytes ...

  8. 【UTR #2】题目排列顺序

    题目描述 "又要出题了." 宇宙出题中心主任 -- 吉米多出题斯基,坐在办公桌前策划即将到来的 UOI. 这场比赛有 $n$ 道题,吉米多出题斯基需要决定这些题目的难度,然后再在汪 ...

  9. UVA - 10972 RevolC FaeLoN

    一道特别好的题qwq. 题目大意就是给你一个无向图,让你把边定向之后再加一些边使得这个图强连通,求最少需要加多少边. 一开始毫无头绪23333,数据范围让人摸不着头脑..... 然后开始画图,,,发现 ...

  10. PHP微信公众平台OAuth2.0网页授权,获取用户信息代码类封装demo(二)

    一.这个文件微信授权使用的是OAuth2.0授权的方式.主要有以下简略步骤: 第一步:判断有没有code,有code去第三步,没有code去第二步 第二步:用户同意授权,获取code 第三步:通过co ...