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. 实验一 cmd命令的编写

    一.目的和要求 1. 实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语言编程初步. 2.实验要求 编写类似于DOS,UNIX的命令行解释程序 (1)自行定义 ...

  2. charles抓包工具

    HTTP抓包 打开Charles程序 查看Mac电脑的IP地址,如192.168.1.7 打开iOS设置,进入当前wifi连接,设置HTTP代理Group,将服务器填为上一步中获得的IP,即192.1 ...

  3. Ubuntu上安装MySql过程,以及遇到的一些问题

    今天在Ubuntu服务器上安装MySql的时候遇到了一些问题,记录下来,以防以后忘记. 安装环境:Ubuntu14.04 安装命令: //安装Mysal服务端//会提示输入root密码 sudo ap ...

  4. 不支持关键字“data source”

    网上大部分都是说data source之间需要插入一个空格或者都是一些低级的拼写错误造成的,但是我没有出现这些情况,是通过把data source改成server解决的,具体config里面的代码如下 ...

  5. js动画之多物体运动

    多个物体这不能使用一个定时器了,要给每个物体一个定时器 <!DOCTYPE html> <html lang="en"> <head> < ...

  6. Android 振动器

    今天介绍一下Android的振动器Vibrator,有三个方法来控制手机振动: 1.void vibrate(long milliseconds):控制手机振动milliseconds毫秒. 2.vo ...

  7. Linux Shell脚本编程--Head/Tail命令详解

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾,看看下面的范例:## ( ...

  8. c++虚函数和内联构造函数

    创建一个含有虚函数的对象时, 编译器会实现 "初始化其VPTR以指向相应的VTABLE" 这个操作 ,而实现这个操作是通过 "插入隐藏代码至构造函数中" 故此时 ...

  9. spring实现一对多表单的保存

    现在我已经从ios转行为java了呦吼吼吼~~~ 1.先来一张界面,项目的字段太多了,为了研究知识点我做了个潘长江版的

  10. Linux内核分析——理解进程调度时机跟踪分析进程调度与进程切换的过程

    20135125陈智威 +原创作品转载请注明出处 +<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验 ...