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?

Example

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

return 3

For the problem, try to think about it in this way.

Firstly, we define the problem of DP(n) as the ways of approaching to n stairs.

The problem of DP(n) depends on DP(n-1) and DP(n-2). Then the DP(n) = DP(n-1) + DP(n-2). Because there is two possibilities for DP(n) happen due to the rule that either it is accomplished by step 1 or step 2 stairs before approaching to n.

Initialize the DP(0) =1 and DP(1) = 1.

Solve problem DP(n)

 public class Solution {
/**
* @param n: An integer
* @return: An integer
*/
public int climbStairs(int n) {
// write your code here
if (n <= 1) {
return 1;
}
int last = 1, lastlast = 1;
int now = 0;
for (int i = 1; i < n; i++) {
now = last + lastlast;
lastlast = last;
last = now;
}
return now;
}
}

Then this problem is a fibonacci sequence

LintCode Climbing Stairs的更多相关文章

  1. [LintCode] 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] 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: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  4. 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 ...

  5. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  6. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  7. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

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

  8. 【LeetCode练习题】Climbing Stairs

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

  9. 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 ...

随机推荐

  1. 【PCB】【项目记录】AWG任意波形产生器

    ———————————————————————————————————————————————————————————————————— 原理图V1.0 01.top 已完成原理图绘制,但有相当错误, ...

  2. python类型转换、数值操作

    类型转换 代码  1 函数                      描述  2 int(x [,base ])         将x转换为一个整数  3 long(x [,base ])       ...

  3. C#获取实体类属性名称

    方法: public static string GetPropertyName(Expression<Func<SupplierInfos, string>> expr) { ...

  4. 为bootstrap添加更多自定义图标

    From: http://blog.csdn.net/mengxiangfeiyang/article/details/45224731 Twitter Bootstrap 真是前端开发的瑞士军刀,作 ...

  5. 支付宝APP支付后台参数生成Java版(一)

    一.支付参数组装: String[] parameters={ "service=\"mobile.securitypay.pay\"",//固定值 " ...

  6. 基本ASP的语法规则

    1.ASP 文件能够包含服务器端脚本,这些脚本被分隔符 <% 和%> 包围起来. 服务器脚本在服务器上执行,可包含合法的表达式.语句.或者运算符. 向浏览器写输出,用命令response. ...

  7. java开发环境搭建

    回顾环境安装的流程. 安装jdk 配置java开发环境 配置path以及classpath. 我是在http://www.oracle.com/上下载的.点击Downloads-->java f ...

  8. STM32——外部中断EXIT实现

    外部中断实现步骤: 一.初始化,包括:1.AFIO时钟中断和GPIO时钟使能:                          2.GPIO初始化                           ...

  9. iptables基本规则配置(二)

    注释:文章中fg:为示例  红色标记的为命令 在上篇博文中详细讲解了iptables的原理及一些常用命令,这里在简要的说明一下: Linux防火墙包含了2个部分,分别是存在于内核空间的(netfilt ...

  10. JSON入门学习

    JSON是一种与开发语言无关的轻量级的数据格式(JavaScript Object Notation) 优点:易于阅读和编写,易于程序解析和生产 JSON数据格式中没有日期及时间的数据格式的.一般直接 ...