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. CURL学习总结(1)

    1.curl是什么?         百度百科定义:     curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.W ...

  2. Spring Security入门(1-13)Spring Security的投票机制和投票器

    1.三种表决方式,默认是 一票制AffirmativeBased public interface AccessDecisionManager { /** * 通过传递的参数来决定用户是否有访问对应受 ...

  3. C语言 左值、右值

    左值就是在赋值中可以放在赋值操作符两边的值 右值则是只可以放在赋值操作符右边的值 ++i是直接给i变量加1,然后返回i本身,因为i是变量,所以可以被赋值,因此是左值表达式i++现产生一个临时变量,记录 ...

  4. POJ-1125 Stockbroker Grapevine---Floyd应用

    题目链接: https://vjudge.net/problem/POJ-1125 题目大意: 股票经纪人要在一群人中散布一个谣言,而谣言只能在亲密的人中传递,题目各处了人与人之间的关系及传递谣言所用 ...

  5. scrollTop doesn't scroll on Chrome 61

    在chrome61 不支持滚动 解决方案: Use document.scrollingElement if supported, and fall back to the current code. ...

  6. scrapy的命令行

    scrapy --help 列出帮助信息以及常用命令scrapy version 列出scrapy版本scrapy version -v 列出详细的scrapy版本以及各组件信息 scrapy sta ...

  7. 关于字数太多直接变成省略号的方法css

    文字超出限制的宽度自动隐藏,并且变为省略号 这是之前写的,现在要做一个两行的 于是万能找百度,居然真的有这个方法: 于是,我就变成了搬运工:○( ^皿^)っHiahiahia- http://blog ...

  8. 网络流入门-POJ1459PowerNetwork-Dinic模板

    (我有什么错误或者你有什么意见,欢迎留言或私聊!谢谢!) (Ps:以前听说过网络流,想着以后再学,这次中南多校赛也碰到有关网络流的题目,想着这两天试着学学这个吧~~ 这是本人网络流入门第二题,不知道怎 ...

  9. div中的内容超过容器宽度的问题

    问题描述: <div class="category">    <div class="column-a">排名</div> ...

  10. [LeetCode] K Empty Slots K个空槽

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...