Climbing Stairs

https://oj.leetcode.com/problems/climbing-stairs/

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?

这题比较简单,可以使用动态规划来求解。请看以下分析:

State:f[i] 表示从起点出发达到第 i 个位置的方案总数

Function:由于第 i 个位置可以由第 i – 2 个位置走两步或者由第 i – 1 个位置走一步而到达,因此有以下状态转移方程:

f[i] = f[i-1] + f[i-2]

Initialize:1. 从起点走到第一个位置,显然只有走 1 步到达这一种方案。

2. 从起点走到第二个位置,有两种方案:直接走 2 步或者每次走 1 步,走 2 次。因此,初始化状态如下:

f[0] = 1

f[1] = 2

注意:数组下标从0开始。

Answer:f[n - 1] 。

下面为 AC 的代码:

/**
* Author : Zhou J
* Email : zhoujx0219@163.com
*/ class Solution {
public:
int climbStairs(int n)
{
if (n == 0)
{
return 0;
} // State: 从起点走到第 i 个位置的方案总数
int sum[n]; // initialize
sum[0] = 1;
if (n >= 2)
{
sum[1] = 2;
} // switch the state
for (size_t ix = 2; ix < n; ++ix)
{
sum[ix] = sum[ix - 1] + sum[ix - 2];
} return sum[n - 1];
}
};

Optimize

当然,此处并不需要使用一个 n 维的数组来存放 State ,观察状态转移方程就可以知道,此处只需要两个变量来存放状态即可。因此下面的代码对空间做了进一步的优化:

/**
* Author : Zhou J
* Email : zhoujx0219@163.com
*/ class Solution {
public:
int climbStairs(int n)
{
if (n <= 2)
{
return n;
} size_t now;
        size_t lastlast = 2; // f[1]
size_t last = 1; // f[0] // switch the state
for (size_t ix = 2; ix < n; ++ix)
{
now = lastlast + last;
last = lastlast;
lastlast = now;
} return now;
}
};

[LeetCode] Climbing Stairs (Sequence DP)的更多相关文章

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

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

  2. LeetCode算法题-Min Cost Climbing Stairs(Java实现)

    这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...

  3. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

    翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...

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

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

  5. LeetCode算法题-Climbing Stairs(Java实现)

    这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...

  6. LeetCode Unique Paths (简单DP)

    题意: 给出一个m*n的矩阵,robot要从[1][1]走到[m][n],每次只能往下/右走,问有多少种走法? 思路: DP的经典问题.先将[1][1]设为1,然后两种走法就是分别用[i][j]去更新 ...

  7. 力扣—climbing stairs(爬楼梯) python实现

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

  8. leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

    leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...

  9. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

随机推荐

  1. ubuntu下搭建testlink

    环境配置: 1. 安装mysql 教程网上找 2. 安装apache sudo apt-get install apache2 重启apache服务 sudo /etc/init.d/apache2 ...

  2. JSON在各种语言中的运用

    1. JSON数据格式 JSON(JavaScript Object Notation) 从结构上看,所有的数据最终都可以分成三种类型:  第一种类型是scalar(标量),也就是一个单独的strin ...

  3. iOS - xcode - label 字体自动根据宽高 显示完全

    1. label 左右约束要给.  2.代码实现label.adjustsFontSizeToFitWidth = YES

  4. [leetcode]228. Summary Ranges区间统计

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  5. 9-sort使用时的错误

    /*                                              矩形嵌套 题目内容: 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形 ...

  6. 深入了解 php 底层机制 (-)洪定坤

  7. SSL - 简介

    一.密码技术 要了解SSL协议,首先要了解:加密算法.消息摘要算法(又称为哈希算法Hash),数字签名等概念.这些技术每个都可以写出一整本的书,它们结合在一起,提供了保密性.完整性和身份验证的功能. ...

  8. 谈谈我对Ui设计师的一些观点

    做ui设计师3年多了,对ui设计师在工作中也了解了许多. 作为UI设计师,在工作中需要清楚了解设计的目的,尤其是你做的不是大众化产品,不能以个人认知.很强的主题性来确定. 例如针对儿童人群的app时, ...

  9. 使用kindeditor 4.1.7 编辑器 注意事项,上传图片失败 问题 ,

    <script charset="utf-8" src="editor/kindeditor.js"></script> <scr ...

  10. 修改Swing窗口风格

    String look; java:    look = "javax.swing.plaf.metal.MetalLookAndFeel"; Windows:    look = ...