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时,有1种方法,即直接走1步

当n=2时,有2方法:连续走2步,或直接走两步

对于n,设f(n)为总方法,则 f(n) = f(n-1)+f(n-2)

ps:f(n-1)即第一次走一步的走法,

f(n-2)即第一次走两步的走法

归回fibnacci问题解法:

 class Solution {
public:
int climbStairs(int n) {
if(n < )
return -;
int res[] = {,};
if(n<)
return res[n]; int fib1 = ;
int fib2 = ; int result = ; for(int i = ; i <= n ; i++){
result = fib1 + fib2;
fib1 = fib2;
fib2 = result;
} return result;
}
};

Climbing Stairs 爬楼梯问题,每次可以走1或2步,爬上n层楼梯总方法 (变相fibonacci)的更多相关文章

  1. n阶楼梯,一次走1,2,3步,求多少种不同走法

    ##已知n阶楼梯,一次可以迈1,2,3步.求所有走法## 如果要列出走法,时间复杂度太高,O(n)=2**n,前两个函数遍历走法.## 如果只是单纯列出走法数量,就简单多了,也但是很容易内存爆表. # ...

  2. [Leetcode] climbing stairs 爬楼梯

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

  3. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  4. 70. Climbing Stairs【leetcode】递归,动态规划,java,算法

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

  5. C++练习 | 掷骰子走到第n步的方法数(DFS)

    玩家根据骰子的点数决定步数,骰子点数为1的时候走一步,以此类推.求玩家走到第n步总共有多少种投骰子的方法.输入为一个整数n,输出为投骰子的方法数. #include <iostream> ...

  6. [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 ...

  7. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  8. 爬楼梯 · Climbing Stairs

    [抄题]: 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? [思维问题]: 不知道一步.两步怎么加.还是用iteration迭代.此题公式可被 ...

  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. #Go# 常用类型转换

    #string 2 int int, err := strconv.Atoi(string) #string 2 int64 int64, err := strconv.ParseInt(string ...

  2. C#以管理员权限运行源码,C#软件获取管理员权限,c#获取管理员权限

    C#以管理员权限运行源码,C#软件获取管理员权限,c#获取管理员权限 发布时间:2014-10-19 21:40内容来源:未知 点击: 次 windows 7和vista提高的系统的安全性,同时需要明 ...

  3. 第2章—装配Bean—自动化装配Bean

    自动化装配Bean 2.1.Spring配置可选方案 ​ 装配是依赖注入DI的本质,Spring提供了以下三种注入的装配机制: 在XMl中进行显式配置 在java中进行显式配置 隐式的Bean发现机制 ...

  4. CentOS6.4安装OpenSSL

    1.下载 wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz 2.解压 tar zxf openssl-1.0.2h.tar.gz cd ...

  5. 1-1、create-react-app 配置 mobx

    1.用npx create-react-app my-app安装项目 2.cd my-app 3.执行 npm  run eject  让配置文件可见 4.npm install --saveDev ...

  6. 设计模式--策略模式(strategy)

    1.策略模式(strategy ['strætədʒi]) 我的理解是:方案候选模式 (反正关键就是有很多的候选,哈哈) 看了很多例子,都是在说鸭子的,那个例子很好,在这里可以看 他们生产鸭子,我们就 ...

  7. Spring源码深度解析系列-----------org.springframework.aop-3.0.6.RELEASE

    Spring源码深度解析系列-----------org.springframework.aop-3.0.6.RELEASE

  8. ibatis实现批处理

    最近做一个小项目,用到Spring+iBatis.突然遇到一个很久远,却很实在的问题:在Spring下怎么使用iBatis的批处理实现? 大概是太久没有写Dao了,这部分真的忘得太干净了. 从4个层面 ...

  9. R语言查找变量ls函数

    要知道目前在工作区中的可用变量,可以使用 ls()函数列出所有变量. 另外,ls() 函数可以使用模式来匹配变量名称. print(ls()) 当上面的代码执行时,它产生以下结果: [1] " ...

  10. WEB Front-end Development Technology

    1.Einführung der HTML(Hypertext Markup Language) 1.1 Grundlegendes Label 1.1.1 Block Label <h1> ...