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. xsy 1790 - 不回头的旅行

    from NOIP2016模拟题28 Description 一辆车,开始没油,可以选择一个点(加油站)出发 经过一个点i可加g[i]的油,走一条边减少len的油 没油的时候车就跪了 特别的,跪在加油 ...

  2. Java-河内塔问题

    河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edouard Lucas曾 ...

  3. display的32种写法--摘抄

    你知道『回』字有四种写法,但你知道display有32种写法吗?今天我们一一道来,让你一次性完全掌握display,从此再也不用对它发愁. 从大的分类来讲,display的32种写法可以分为6个大类, ...

  4. Charger Warning Message

    使用 PMIC_RGS_VCDT_HV_DET 判斷 charger 是否有 ovp. LV_VTH : 4.15V

  5. ASP 500错误解决方法

    最有效的解决方法: 经  c:\windows\temp 目录增加everyone写权限. 环境: windows2008

  6. LeetCode OJ——Climbing Stairs

    http://oj.leetcode.com/problems/climbing-stairs/ 走台阶的题目,转换出数学模型之后,就是Fibonacci数列.后一个数等于前两个数的和. 递归版本超时 ...

  7. Django中使用表单

    使用表单 表单用 user 提交数据,是网站中比较重要的一个内容 GET 和 POST 方法 GET 和 POST 的区别 URL,全称是"统一资源定位符".用于对应互联网上的每一 ...

  8. http错误种类及原因

    http://blog.csdn.net/dxykevin/article/details/50950878 [摘要]HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应 ...

  9. chattr&chown&cat&cut&useradd&passwd&chage&usermod

    1.用chattr命令防止系统中某个关键文件被修改 chattr +i /etc/resolv.conf chattr -i /etc/resolv.conf 要想修改此文件就要把i属性去掉 lsat ...

  10. SSM整合案例

    使用IDEA整合SSM spring核心配置文件:beans_core.xml/applicationContext.xml <?xml version="1.0" enco ...