题目来源:

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


题意分析:

  爬楼梯,一次可以爬一步或者两步。如果要爬n层,问一共有多少种爬法。比如说,如果是3层,可以有[[1,1,1],[1,2],[2,1]]共3种方法。


题目思路:

  这是一个典型的动态规划问题。n层的方法数 = n - 1层的方法数 + n - 2层的方法数。


代码(Python):

  

 class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1 or n == 0:
return 1
i,tmp1,tmp2 = 2,1,1
while i <= n:
tmp1 = tmp1 + tmp2
if i == n:
return tmp1
i += 1
tmp2 = tmp1 + tmp2
if i == n:
return tmp2
i += 1

转载请注明出处:http://www.cnblogs.com/chruny/p/5045540.html

[LeetCode]题解(python):070-Climbing Stairs的更多相关文章

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

  2. [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  3. LeetCode 70. 爬楼梯(Climbing Stairs)

    70. 爬楼梯 70. Climbing Stairs 题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意: 给定 ...

  4. LN : leetcode 746 Min Cost Climbing Stairs

    lc 746 Min Cost Climbing Stairs 746 Min Cost Climbing Stairs On a staircase, the i-th step has some ...

  5. LeetCode练题——70. Climbing Stairs

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

  6. 【LeetCode】070. Climbing Stairs

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

  7. Java for LeetCode 070 Climbing Stairs

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

  8. LeetCode之“动态规划”:Climbing Stairs

    题目链接 题目要求 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eit ...

  9. 070 Climbing Stairs

    你正在爬楼梯.需要 n 步你才能到达顶部.每次你可以爬 1 或 2 个台阶.你有多少种不同的方式可以爬到楼顶呢?注意:给定 n 将是一个正整数.示例 1:输入: 2输出: 2说明: 有两种方法可以爬到 ...

  10. LeetCode(70) Climbing Stairs

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

随机推荐

  1. 计算机网络--http代理server的设计与实现

    一.Socket编程的client和服务端的主要步骤: Java Socket编程:对于http传输协议 client: 1.创建新的socket,绑定serverhost和port号 2.Socke ...

  2. DataTable去除重复行

    //抽取dt中的特定列"Region","Cur","Year"组合成新的dat DataTable dat = dt.DefaultVie ...

  3. 玩转kindle paperwhite: 如何越狱,安装强大外挂软件koreader

    NOTICE 1: 在更新kpvbooklet和使用最新版本的koreader(v2013.03-211)时候,会出现pdf文档无法重排的错误.亲测. 如果你是使用的最新版本koreader且出现上述 ...

  4. hbase 单机安装问题

    报zookeeper exception not found I fixed this by editing the file "/usr/local/hbase-0.94.1/conf/h ...

  5. C++ Member Functions的各种调用方式

    [1]Nonstatic Member Functions(非静态成员函数) C++的设计准则之一就是:nonstatic member function至少必须和一般的nonmember funct ...

  6. Labview中引用,属性节点,局部变量之间的区别

    Labview中引用,属性节点,局部变量之间的区别        在Labview中我们经常会碰到这样几个概念,那就是控件的引用,属性节点以及局部变量,他们之间到底有哪些区别呢?        首先谈 ...

  7. css样式写一个三角形

    <style> .test{ border-color:transparent #abcdef transparent transparent; border-style:solid; b ...

  8. codeforces 245H . Queries for Number of Palindromes 区间dp

    题目链接 给一个字符串, q个询问, 每次询问求出[l, r]里有多少个回文串. 区间dp, dp[l][r]表示[l, r]内有多少个回文串. dp[l][r] = dp[l+1][r]+dp[l] ...

  9. JavaScript 的 Promise

    先看这个 http://www.html5rocks.com/zh/tutorials/es6/promises/#toc-api  [JavaScript Promise 浏览器支持的Promise ...

  10. DelphiXe5中的双向绑定(使用使用TBindScope和TBindExpression,并覆盖AfterConstruction函数)

    在Delphi下等这一功能很久了,虽然C#下早已实现了这一功能.但是在Dephi下尝试这项功能时还是有些许的激动.闲言少絮,直接上代码. unit BindingDemo; interface use ...