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?

Note: Given n will be a positive integer.

Example 1:

  1. Input: 2
  2. Output: 2
  3. Explanation: There are two ways to climb to the top.
  4. 1. 1 step + 1 step
  5. 2. 2 steps

Example 2:

  1. Input: 3
  2. Output: 3
  3. Explanation: There are three ways to climb to the top.
  4. 1. 1 step + 1 step + 1 step
  5. 2. 1 step + 2 steps
  6. 3. 2 steps + 1 step

分析

这道题是斐波那契数列的模型,上第n阶台阶的方法数是f(n)=f(n-1)+f(n-2),因为到第n阶台阶只有从第n-1阶台阶上来或者从第n-2阶上来;

  1. class Solution {
  2. public:
  3. int climbStairs(int n) {
  4. int fab[n+1];
  5. fab[0]=fab[1]=1;
  6. for(int i=2;i<=n;i++){
  7. fab[i]=fab[i-1]+fab[i-2];
  8. }
  9. return fab[n];
  10. }
  11. };

70. Climbing Stairs(动态规划)的更多相关文章

  1. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  2. Leetcode之70. Climbing Stairs Easy

    Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...

  3. 42. leetcode 70. Climbing Stairs

    70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...

  4. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  5. 377. Combination Sum IV 70. Climbing Stairs

    back function (return number) remember the structure class Solution { int res = 0; //List<List< ...

  6. 刷题70. Climbing Stairs

    一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...

  7. LeetCode练题——70. Climbing Stairs

    1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...

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

  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. [Python] partial改变方法默认參数

    Python 标准库中 functools库中有非常多对方法非常有有操作的封装,partial Objects就是当中之中的一个,他是对方法參数默认值的改动. 以下就看下简单的应用測试. #!/usr ...

  2. android studio中xml文件代码提示问题

    在系统控件中输入“a”能提示出android:id等所有属性.而在第三方库的控件中输入“a”只会提示“appNs”,但如果手动写app:id="@+id/aaa"系统也是可以识别的 ...

  3. 布局技巧2:合并布局(merge标签)

    我们已经有文章向你描述如何使用<include />标签来重用和共享你的布局代码.这篇文章将向你阐述<merge />标签的使用以及如何与<include />标签 ...

  4. 【题解】动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393]

    [题解]动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393] 水一水QAQ 题目链接: \([P3157]\) \([BZOJ3295]\) [题目描述] 对于一个序 ...

  5. flask web 表单验证 WTForms

    简介 WTForms 是一个flask集成框架,或者说是库,用于处理浏览器表单提交的数据,它在flask-WTF的基础上扩展并添加了一些随手可得的精巧帮助函数,这些函数将会是在flask里使用表单更加 ...

  6. ACM_“IP地址”普及(进制转换)

    “IP地址”普及 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大家都知道最近广财大校园网提速,现在就跟大家普及一下简单的互联网 ...

  7. Select2插件ajax方式加载数据并刷新页面数据回显

    今天在优化项目当中,有个要在下拉框中搜索数据的需求:最后选择使用selec2进行开发: 官网:http://select2.github.io/ 演示: 准备工作: 文件需要引入select2.ful ...

  8. Web开发中跨域的几种解决方案

    同domain(或ip),同端口,同协议视为同一个域,一个域内的脚本仅仅具有本域内的权限,可以理解为本域脚本只能读写本域内的资源,而无法访问其它域的资源.这种安全限制称为同源策略. 出于安全考虑,HT ...

  9. fcc 响应式框架Bootstrap 练习1

    需要通过添加下列代码到你的HTML开头来将Bootstrap添加到任意应用中: <link rel="stylesheet" href="//cdn.bootcss ...

  10. android shape stroke只绘制一边或者某几边

    <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=" ...