题目来源:

  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. as3 与js相互通信

    1.as和js通讯实例 如果as3调用 JAVA script 中的函数直接在as中添加 if (ExternalInterface.available) {       ExternalInterf ...

  2. Flash,EEPROM差别

    flash是用来存储代码的,在执行过程中不能改:EEPROM是用来保存用户数据,执行过程中能够改变,比方一个时钟的闹铃时间初始化设定为12:00,后来在执行中改为6:00,这是保存在EEPROM里,不 ...

  3. 解密:LL与LR解析 2(译,完结)

    由于GFW,我无法联系到作者,所以没有授权,瞎翻译的.原文在这里[http://blog.reverberate.org/2013/07/ll-and-lr-parsing-demystified.h ...

  4. angular的post传参后台php无法接收

    很多时候我们需要用ajax提交post数据,angularjs与jq类似,也有封装好的post. 但是jQuery的post明显比angularjs的要简单一些,人性化一些. 两者看起来没什么区别,用 ...

  5. 电商网站前端架构#1 多页面 vs 单页面

    课程地址:http://www.imooc.com/learn/186 讲师:远人 课程基础: html css js ,做过一些项目. 多页面 我们往常使用的web服务大多是多页面形式,依靠后端的架 ...

  6. mywebsite1

    http://www.wuweierwei.com/ 个人网站 http://www.yyyweb.com/demo/icons-filling/index.html CSS3图标填充效果 http: ...

  7. 从VS转MyEclipse的15天使用体验

    脱离了VS强大的IDE功能之后,转向MyEclipse,发现很大差别,Java的IDE对比VS感觉弱很多,而且树形没有那么好用,Java里面是以包为主,区别与C#的最大就是,高亮提示关键字,这一点Ja ...

  8. 征服 Redis + Jedis + Spring —— 配置&常规操作

    Spring提供了对于Redis的专门支持:spring-data-redis.此外,类似的还有: 我想大部分人对spring-data-hadoop.spring-data-mongodb.spri ...

  9. 深入理解java虚拟机系列二——垃圾收集算法

    在主流的商用程序语言中大多都是用根搜索算法(GC Roots Tracing)判断对象是否存活,比如java,c#等.当从GC Roots到某个对象不可达,则证明此对象是不可用的,将要被回收. 商业虚 ...

  10. git 使用随笔

    /*将远端库git@github.com:myrepo/base.git从远端clone到本地*/git clone git@github.com:myrepo/base.git /*克隆版本库的时候 ...