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?

思路:这道题是斐波那契数列的延伸。首先用最简单的递归的方法

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

不出意料的超时了。替代递归的方法是用动态规划。

class Solution {
public:
int climbStairs(int n)
{
vector<int> res(n+);
res[] = ;
res[] = ;
for (int i = ; i <= n; i++)
{
res[i] = res[i-] + res[i-];
}
return res[n];
} };

时间复杂度降低了,接下来降低空间复杂度。用变量代替数组

class Solution {
public:
int climbStairs(int n)
{
if (n <= ) return n;
int f1 = ;
int f2 = ;
int f3 = ;
for (int i = ; i <= n; ++i) {
f3 = f2 + f1;
f1 = f2;
f2 = f3;
} return f3;
} };

最终的时间复杂度O(n),空间复杂度O(1)

[LeetCode] Climbing Sairs的更多相关文章

  1. [LeetCode] Climbing Stairs 爬梯子问题

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

  2. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  3. LeetCode——Climbing Stairs

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

  4. [LeetCode] Climbing Stairs (Sequence DP)

    Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...

  5. LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)

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

  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] Climbing Stairs 斐波那契数列

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

  8. leetcode Climbing Stairs python

    class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. 20155234 2006-2007-2 《Java程序设计》第4周学习总结

    20155234 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 为了避免重复的行为定义使用继承. 要学会如何正确判断使用继承的时机以及继承之后如何活用多态. ...

  2. 在Linux中安装JDK和IDEA

    前言 寒假安装虚拟机的时候我就没有安装好,到学校之后,因为时间紧加上更习惯Windows的操作习惯,我只在Windows上安装了JDK和IDEA,但是随着学习的深入,我发现用虚拟机写命令行.新建jav ...

  3. 20155322 2016-2017-2 《Java程序设计》第2周学习总结

    20155322 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 本周按照教学安排学习教材的第三章,下面简单的概括一下我的学习总结: 第三章的主要内容是有关于 ...

  4. 20155327 实验一《Java开发环境的熟悉》实验报告

    实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验知识点 JVM.JRE.JDK的安装位置与区别: 命令行运行javac:jav ...

  5. Nginx入门篇(五)之LNMP环境应用

    一.LNMP组合工作原理 (1)用户通过浏览器输入域名请求Nginx web服务: (2)Nginx对请求的资源进行判断,如果是静态资源,则由Nginx返回给用户:如果是动态请求(.php文件),那么 ...

  6. 【jQuery学习】用JavaScript写一个输出多选框的个数报错:Cannot set property 'onclick' of null"

    说明:代码段来源于:<锋利的jQuery> 根据代码段我补充的代码如下: <!DOCTYPE html> <html> <head> <meta ...

  7. Unity CombineTexture

    public Texture2D CombineTexture(Texture2D background, Texture2D top) { int width = background.width; ...

  8. Swift入门基础知识

    var //代表变量,变量的值可以改变 let//代表常量类型不可改变 //声明常量heh类型Swift会自动根据你的值来自动判断该变量的类型也可以指定类型(个人感觉还是指定类型的比较好,可能会减少系 ...

  9. shell 判断日期间隔及润年

    #!/bin/bash test.sh until echo "----------------------------------" echo "请输入您的选择:&qu ...

  10. vue cli 3 +jquery

    const webpack = require('webpack')module.exports = { // baseUrl type:{string} default:'/' // 将部署应用程序 ...