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阶楼梯,每次可以走一步或者两步,总共有多少种方法。

思路:动态规划。维护一个一维数组dp[n+1],dp[0]为n=0时的情况,dp[ i ]为到达第i阶台阶总共的方法。例,当n=4时,如下图,很快就可以推出状态转移方程为:dp[i]=dp[i-1]+dp[i-2] (i >=2)。

代码如下:

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

[Leetcode] climbing stairs 爬楼梯的更多相关文章

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

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

  3. [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

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

  4. [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失

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

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

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

  6. climbing stairs(爬楼梯)(动态规划)

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

  7. Climbing Stairs爬楼梯——动态规划

    题目描写叙述: 初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层. 请问爬到第n层有多少种不同的方法? 进阶:假设每次能够爬两层.和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种 ...

  8. LeetCode 70. Climbing Stairs爬楼梯 (C++)

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

  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. SVM中的间隔最大化

    参考链接: 1.https://blog.csdn.net/TaiJi1985/article/details/75087742 2.李航<统计学习方法>7.1节 线性可分支持向量机与硬间 ...

  2. 50条大牛C++编程开发学习建议

    每个从事C++开发的朋友相信都能给后来者一些建议,但是真正为此进行大致总结的很少.本文就给出了网上流传的对C++编程开发学习的50条建议,总结的还是相当不错的,编程学习者(不仅限于C++学习者)如果真 ...

  3. 洛谷U32670 小凯的数字(比赛)

    题目网址 https://www.luogu.org/problemnew/show/U32670 题目背景 NOIP2018 原创模拟题T1 NOIP DAY1 T1 or DAY 2 T1 难度 ...

  4. [Cracking the Coding Interview] 4.4 Check Balanced

    Implement a function to check if a binary tree is balanced. For the purpose of this question, a bala ...

  5. C++11中std::bind的使用

    std::bind: Each argument may either be bound to a value or be a placeholder: (1).If bound to a value ...

  6. guacamole实现剪切复制

    主要功能是实现把堡垒机的内容复制到浏览器端,把浏览器端的文本复制到堡垒机上. 借助一个中间的文本框,现将堡垒机内容复制到一个文本框,然后把文本框内容复制出来.或者将需要传递到堡垒机的内容先复制到文本框 ...

  7. 如何激活win10

    第一步:用管理员权限打开命令提示符: 第二步:输入命令---slmgr.vbs /upk                          (成功卸载了产品密钥) 第三步:slmgr /ipk NPP ...

  8. Android adb shell启动应用程序的方法

    在Android中,除了从界面上启动程序之外,还可以从命令行启动程序,使用的是命令行工具am. usage: am [subcommand] [options] start an Activity: ...

  9. Ubuntu-C++环境设置

    在学习C++,顺便想熟悉一下Linux下开发 所以就开始搭建Linux环境 第一步就是下载虚拟机和Ubuntu 虚拟机 vm 12.1 Ubuntu 16 VM安装省略 Ubuntu安装省略 下面是我 ...

  10. Lua工具类

    1.打印table --一个用以打印table的函数 function print_r (t, name) print(pr(t,name)) end function pr (t, name, in ...