题目:

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. 20190814-A Second

    一秒,或是,第二次? 这考试也太频繁了吧…… 考试过程: 看三道题. T1没思路. 然后去厕所清醒了一下. 在厕所的时候,突然想到可以离散化. 于是就这么搞了. 然后去写T2. T2好像是数学题. 于 ...

  2. light oj 1427(ac自动机)

    #include <bits/stdc++.h> using namespace std; *; ; map<string,int>Map; struct Trie { int ...

  3. 洛谷 P1036 选数【背包型DFS/选or不选】

    题目描述 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别为 3,7,12, ...

  4. 禁用/移除WordPress页面的评论功能

    对于某些类型的WordPress站点,也许不需要在页面(page)提供评论功能,那么你可以通过下面的方法,很容易就禁用或移除WordPress页面的评论功能. 方法1:在页面编辑界面取消该页面的评论功 ...

  5. 那些年,我们见过的Java服务端乱象

    导读 查尔斯·狄更斯在<双城记>中写道:“这是一个最好的时代,也是一个最坏的时代.”移动互联网的快速发展,出现了许多新机遇,很多创业者伺机而动:随着行业竞争加剧,互联网红利逐渐消失,很多创 ...

  6. PHP 学习1.0

    1.简单一个class 类: 获取表单提交的值 采用post方式 <html><head><title>PHP TEST</title></hea ...

  7. web前端学习(二)html学习笔记部分(1) -- html5新增的元素及特性等等

    检查,在浏览器中可以调整设备类型 html5实现水池效果. lang:en为英文语言,中文语言zh <html lang="en"> <head> < ...

  8. 对于MD5加密处理方式

    来源:http://blog.51cto.com/xqtesting/1924977 但有时候我们请求的参数可能需要加密,比如登录接口中的密码可能需要经过md5加密这时候怎么处理呢? 这种方法比较简单 ...

  9. 26718汉字,gbk是23940个汉字,gb18030有76556个汉字

    1 a 厑 吖 呵 啊 嗄 嬶 腌 錒 锕 阿 仰 卬 岇 昂 昻 枊 盎 肮 腌 軮 醠 雵 骯 侒 俺 儑 匎 匼 厂 厈 唵 啽 垵 埯 堓 媕 安 屵 岸 峎 峖 广 庵 按 揞 晻 暗 案 ...

  10. 深入探索WebSockets

    WebSockets简介 在2008年中期,开发人员Michael Carter和Ian Hickson特别敏锐地感受到Comet在实施任何真正强大的东西时所带来的痛苦和局限. 通过在IRC和W3C邮 ...