题目:

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? (Easy)

分析:

经典的爬楼梯问题,对于第n阶可以从n - 1阶跳一步上来,也可以从n - 2阶跳两步上来,设dp[n]为到第n阶的方案数,

则dp[n] = dp[n - 1] + dp[n - 2]。

不过实现的时候采用自底向上的方式,不要用递归(大量重复计算)。

也算是一道动态规划的入门题吧。

代码:

 class Solution {
public:
int climbStairs(int n) {
if (n == ) {
return ;
}
if (n == ) {
return ;
}
int a = , b = , c = a + b;
for (int i = ; i <= n; ++i) {
c = a + b;
a = b;
b = c;
}
return c;
}
};

LeetCode70 Climbing Stairs的更多相关文章

  1. leetcode70—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 70. 爬楼梯(Climbing Stairs)

    70. 爬楼梯 70. Climbing Stairs 题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意: 给定 ...

  3. LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)

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

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

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

  5. [LintCode] Climbing Stairs 爬梯子问题

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

  6. Leetcode: climbing stairs

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

  7. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  8. Climbing Stairs

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

  9. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

随机推荐

  1. 现在学习 JavaScript 的哪种技术更好:Angular、jQuery 还是 Node.js?(转)

    本文选自<开发者头条>1 月 7 日最受欢迎文章 Top 3,感谢作者 @WEB资源网 分享. 欢迎分享:http://toutiao.io/contribute 这是一个发布在 Quor ...

  2. 数组的方法之(Array.prototype.forEach() 方法)

    forEach() 方法对数组的每个元素执行一次提供的函数. 注意: 没有返回一个新数组 并且 没有返回值! 应用场景:为一些相同的元素,绑定事件处理器! const arr = ['a', 'b', ...

  3. HDU4355 三分查找

    /*  * 三分查找  */ #include<cstdio> #include<cmath> #define eps 1e-6 //typedef __int64 LL; i ...

  4. C# WPF 仿QQ靠近屏幕上方自动缩起功能实现

    碰到了类似需求,但是上网查了一圈发现感觉要么很复杂,要么代码很臃肿,索性自己实现了一个 几乎和QQ是一模一样的效果,而且核心代码只有20行左右. 代码如下: using System; using S ...

  5. CesiumLab 地形数据处理

    最近接连有用户反应地形数据处理的各种问题,我也是各种测试,想想还是整理一个文档彻底说明一下. 地形栅格数据格式,一般是tif ,也有dem或者img,但是我个人强烈建议使用tif格式,因为cesium ...

  6. java的boolean和Boolean

    boolean是基本数据类型Boolean是它的封装类,和其他类一样,有属性有方法,可以new,例如: Boolean bl= new Boolean("true"); // bo ...

  7. MySQLDemo2

    -- 查询所有数据库 show databases -- 删除数据库 drop database a -- use `数据库名称`; 表示使用此数据库 use mybatis -- 查看表结构 sho ...

  8. Docker搭建的MySQL容器出现 "Too many connections 1040" 最大连接数修改完未生效的解决方案

    原文:Docker搭建的MySQL容器出现 "Too many connections 1040" 最大连接数修改完未生效的解决方案 版权声明:本文为博主原创文章,未经博主允许不得 ...

  9. Python要如何实现(列表)排序?

    排序,是许多编程语言中经常出现的问题.同样的,在Python中,如何是实现排序呢?(以下排序都是基于列表来实现) 一.使用Python内置函数进行排序 Python中拥有内置函数实现排序,可以直接调用 ...

  10. 通用、封装、简化 webpack 配置

    通用.封装.简化 webpack 配置 现在,基本上前端的项目打包都会用上 webpack,因为 webpack 提供了无与伦比强大的功能和生态.但在创建一个项目的时候,总是免不了要配置 webpac ...