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?

Have you met this question in a real interview?

Yes
Example

Given an example n=3 , 1+1+1=2+1=1+2=3

return 3

LeetCode上的原题,请参见我之前的博客Climbing Stairs

解法一:

class Solution {
public:
    /**
     * @param n: An integer
     * @return: An integer
     */
    int climbStairs(int n) {
         ) ;
         vector<int> dp(n);
         dp[] = ; dp[] = ;
         ; i < n; ++i) {
             dp[i] = dp[i - ] + dp[i - ];
         }
         return dp.back();
    }
};

解法二:

class Solution {
public:
    /**
     * @param n: An integer
     * @return: An integer
     */
    int climbStairs(int n) {
        , b = ;
        while (n--) {
            b += a;
            a = b - a;
        }
        return a;
    }
};

[LintCode] Climbing Stairs 爬梯子问题的更多相关文章

  1. [LeetCode] 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] 70. Climbing Stairs 爬楼梯

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

  6. LintCode 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(爬楼梯)(动态规划)

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

  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爬楼梯 (C++)

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

随机推荐

  1. Touch ID 实现

    Touch ID 1.要求 机型:iPhone 5s以上 系统:iOS8以上 框架:#import <LocalAuthentication/LocalAuthentication.h> ...

  2. caffe机器学习自带图片分类器classify.py实现输出预测结果的概率及caffe的web_demo例子运行实例

    caffe机器学习环境搭建及python接口编译参见我的上一篇博客:机器学习caffe环境搭建--redhat7.1和caffe的python接口编译 1.运行caffe图片分类器python接口 还 ...

  3. 使用Spring Task轻松完成定时任务

    一.背景 最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用Spring Task进行简单配置完成该需求,本文介绍Spring3.0以后自定义开发的定时任务工具, spring ta ...

  4. BOOST_AUTO宏

    在boost中,有个非常不错的宏BOOST_AUTO(),它的作用是自动给var定义类型,适合function()函数返回的值的类型. int function() { ; } main() { BO ...

  5. Lintcode 9.Fizz Buzz 问题

    ------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...

  6. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  7. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  8. 常用命令[Linux]

    Linux文件类型 -:普通文件(f) d:目录文件 b:块设备文件(block) c:字符设备文件(character) l:符号链接文件(symbolic link file) p:命名管道文件( ...

  9. Bootstrap UI 编辑器

    1. BootSwatchr BootSwatchr 是由 Drew Strickiand 独立开发和维护的,是唯一支持从右到左语言显示的 Bootstrap 自定义构建工具,这也是它的特色之一.Bo ...

  10. html学习第三天—— 第11章 盒子模型 div

    盒模型--边框(一) 盒子模型的边框就是围绕着内容及补白的线,这条线你可以设置它的粗细.样式和颜色(边框三个属性). 如下面代码为div来设置边框粗细为2px.样式为实心的.颜色为红色的边框: div ...