题目:

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. vmware的Linux虚拟机ping不通外网的解决办法

    转载自:https://blog.csdn.net/l_l_b_/article/details/79409843 1.点击此处编辑 2.选择虚拟网络编辑器 3.点击更改设置 4.选择v8 并将使用本 ...

  2. Hdu 1384(差分约束)

    题目链接 Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. linux 调整系统时区

    查看当前时间: date 查看当天详细时区 timedatectl 调整为正确时区 timedatectl set-timezone 'Asia/Shanghai'

  4. Eclipse luna安装 Ivy

    在线安装地址:http://www.apache.org/dist/ant/ivyde/updatesite

  5. JAVA邀请码生成器

    code import java.util.Random; /** * 邀请码生成器,算法原理:<br/> * 1) 获取id: 1127738 <br/> * 2) 使用自定 ...

  6. Oracle存储函数,存储过程

    一.Oracle存储函数:存储的PL/SQL语法块,完成特定的功能.1.语法: 函数关键字: function (1)创建函数 CREATE [OR REPLACE] FUNCTION <fun ...

  7. day38 06-MyEclipse配置Schema约束

  8. 使用log4j打印日志

    在项目中我们必不可少需要打印日志,通过日志我们可以查看系统的运行状态是否正常,当程序出现异常的时候,我们也可以通过查看日志来定位问题的位置,给程序员的工作带来了极大的便利. 以下这边博客的内容是我从一 ...

  9. Phpstrom学习笔记

    1.用*标识编辑过的文件 File - Editor – General - Editor Tabs 选中Mark modifyied tabs with asterisk

  10. jquery 设置 html标签响应式布局

    function sWidth() {//计算当前设备宽度 var widthSize; if ($(window).width() <= 640) { widthSize = $(window ...