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?

Note: Given n will be a positive integer.

https://mp.weixin.qq.com/s/0AgJmQNYAKzVOyigXiKQhA, 动态规划入门最好材料,也是该题个人认为最完美的解释,没有之一! 非常感谢该文章的作者和发给我文章的张春玲老师!

自个代码:

\(O(n)\) time, \(O(1)\) extra space.

// A textbook style question for Dynamic Programming.
// I like it!
int climbStairs(int n) {
// boundarys
if (n == 1) return 1;
if (n == 2) return 2; // state transfer formula
int a = 1, b = 2, temp;
for (int i = 3; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return temp;
}

70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)的更多相关文章

  1. [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming

    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 Easy

    Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...

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

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

  5. 刷题70. Climbing Stairs

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

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

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

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

  8. 377. Combination Sum IV 70. Climbing Stairs

    back function (return number) remember the structure class Solution { int res = 0; //List<List< ...

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

随机推荐

  1. hadoop2.6.0实践:000 虚拟机配置

  2. python入门(6)输入和输出

    python入门(6)输入和输出 输出 >>> print 'hello, world' >>> print 'The quick brown fox', 'jum ...

  3. 如何修改chrome记住密码后自动填充表单的黄色背景 ?

    input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill { background-color: rgb(2 ...

  4. C++中explicit关键字

    explicit: 防止隐式转换使用. 隐式转换:不同类型的变量可以互相转换,如将一个整形数值赋值给一个类,ClassXX  lei = 4: C++中, 一个参数的构造函数(或者除了第一个参数外其余 ...

  5. tk mybatis通用mapper,复杂and or条件查询

    需求:where查询,需要支持(a or b or c) and d 也就是a.b.c三个条件是或的关系,然后再与d相与. 尝试后,可以通过以下方式处理: 方式1:Weekend语法 Weekend& ...

  6. tcpdump记录

    tcpdump -i eth0 -nn -A -X 'host 192.168.20.82 and port 9080' -i:interface 监听的网卡. -nn:表示以ip和port的方式显示 ...

  7. Redis常用命令总结

    在Redis中一共有五种数据类型. 一.String 类型操作 //添加 set key value //查询 get key //删除 del key //拼接 append key value(返 ...

  8. global关键字修改全局变量

    #我们知道全局变量在函数外部,强烈建议不要在函数内部修改全局变量,正常情况下,在函数内部改变全局变量并不影响全局变量的值,举例如下 count = 5 >>> def myfun() ...

  9. CF 472 div1 D. Contact ATC

    #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> # ...

  10. Python3NumPy——数组(1)之创建

    开篇 numpy库作为科学计算的基础库,其地位相当重要,它是对数组操作的基石.它的存在使得线性代数以及矩阵论等相关知识在计算机上的表达更加方便与简单,集中体现出了人想办法,计算机去工作. Python ...