Question

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?

Solution

Classic DP problem. When we climb n steps, we have two choices for last step, either one step or two steps. To save space, we can further improve the solution using O(1) space.

  1. public class Solution {
  2. public int climbStairs(int n) {
  3. if (n < 1)
  4. return 0;
  5. int[] dp = new int[3];
  6. dp[0] = 1;
  7. dp[1] = 1;
  8. for (int i = 2; i <= n; i++) {
  9. dp[2] = dp[0] + dp[1];
  10. dp[0] = dp[1];
  11. dp[1] = dp[2];
  12. }
  13. return dp[1];
  14. }
  15. }

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. 刷题70. Climbing Stairs

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

  3. LeetCode练题——70. Climbing Stairs

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

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

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

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

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

  6. Leetcode: climbing stairs

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

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

  8. Climbing Stairs

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

  9. 3月3日(6) Climbing Stairs

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

随机推荐

  1. JS代码的window.location属性详解

    转载:http://www.5icool.org/a/201105/a564.html 如果你稍微懂一些JS代码,一般都会知道 window.location.href 这个属性.并且用该属性获取页面 ...

  2. PhpForm表单相关的超全局变量操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. HDU 1540 Tunnel Warfare (线段树)

    题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...

  4. Populating Next Right Pointers in Each Node II 解答

    Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...

  5. Maven自定义Archetype

    Maven提供了archetype帮助我们快速构建项目骨架,很便捷.但是,中央仓库中的archetype版本过于陈旧,构建好项目后,需要修改很多信息,甚是麻烦,那么如何自定义个archetype就显得 ...

  6. discuz!NT 常用操作

    一.编辑模版,需在后台模版管理里编辑并提交,这样系统能批量更改相关模版.如:更改登录. 二.config.a 表明:config表示配置文件,a表示配置节名称为a,需在路径 upload_files\ ...

  7. 2D丛林逃生

    游戏介绍: 游戏地图采用二维数组:     每一个小块(Piece)类         上面有一个类型(StuffType)用于判断该小块上面站着的是什么 怪物,玩家,血瓶等等     怪物AI:   ...

  8. UITextField输入长度限制

    [_yourTextField addTarget:self action:@selector(eventEditingChange:) forControlEvents:UIControlEvent ...

  9. hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)

    Problem Description 小明自从告别了ACM/ICPC之后,就開始潜心研究数学问题了,一则能够为接下来的考研做准备,再者能够借此机会帮助一些同学,尤其是美丽的师妹.这不,班里唯一的女生 ...

  10. 3. QT窗体间值的传递(续)

    一.前言 上篇博客中通过重载子窗体的构造函数将主窗体的值传入到子窗体,但是在子窗体运行过程中如何才能将值动态的传入到子窗体?可以有两种办法,1.信号和槽的方式传值:2.主窗体中将传出值设置为publi ...