#Method1:动态规划
##当有n个台阶时,可供选择的走法可以分两类:
###1,先跨一阶再跨完剩下n-1阶;
###2,先跨2阶再跨完剩下n-2阶。
###所以n阶的不同走法的数目是n-1阶和n-2阶的走法数的和
class Solution(object):
    def climbStairs(self, n):
        if n==1 or n==2 or n==0:
            return n
        steps=[1,1]
        for i in xrange(2,n+1):
            steps.append(steps[i-1]+steps[i-2])
        return steps[n]

#Method2:找规律
###当输入为1, 2, 3, 4, 5, 6, 7, 8, 9, 10时,
###观察输出为1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
###斐波那契数列
class Solution(object):
    def climbStairs(self,n):
        pre=cur=1
        for i in xrange(1,n):
            pre,cur=cur,cur+pre
        return cur

【leetcode❤python】70. Climbing Stairs的更多相关文章

  1. [LeetCode&Python] Problem 70. Climbing Stairs

    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

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

  3. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  4. 【LeetCode】70. Climbing Stairs 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 记忆化搜索 动态规划 空间压缩DP 日期 [L ...

  5. 【一天一道LeetCode】#70. Climbing Stairs

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

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

  7. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...

  8. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

  9. 【leetcode❤python】 8. String to Integer (atoi)

    #-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...

随机推荐

  1. cactive信号

    AXI中C-channel的cactive信号并不仅仅应用在CSYSREQ,CSYSACK交互中. CSYSREQ和CSYSACK信号都在低电平表示lower power的请求和应答有效. cacti ...

  2. [php]使用会话session

    <?php /* cookie - 在客户端上存储少了信息(cookie) session(会话) - 解决cookie大小限制/数量/其他 setcookie() - 设置cookie 在客户 ...

  3. 【ibus】设置ibus输入法(pinyin & sunpinyin)

    设置ibus-pinyin 在终端中运行 /usr/lib/ibus-pinyin/ibus-setup-pinyin 命令可以调出ibus的完整设置对话框 设置ibus-sunpinyin 可以执行 ...

  4. Subversion简明手册--使用hook svn

    使用 hook ,为了方便管理员 控制提交的过程 Subversion 提供了 hook 机制.当特定的 事件发生时,相应的 hook 会被调用, hook 其实就相当于特定 事件的处理函数.每个 h ...

  5. UIActionSheet和UIAlert

    UIActionSheet: 首先,在.h文件中添加Protocol,(Protocol相当于Java中的interface) @interface ActionSheetViewController ...

  6. Hibernate,JPA注解@SecondaryTables

    使用类一级的 @SecondaryTable或@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所 ...

  7. Ubuntu 14.04下NFS安装配置

    1.安装nfs-server # apt-get install nfs-kernel-server 2.建立nfs专用文件夹 # mkdir /data/disk1 3.配置nfs # vi /et ...

  8. hdwiki中模板和标签的使用

    MVC中的视图view 主要负责页面显示部分,所有的页面显示全部在此实现,视图对整个页面负责,它通过control的调用来显示页面和数据. ......视图(view)类template.class. ...

  9. 每日一九度之 题目1038:Sum of Factorials

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  10. Spring MVC 中请求返回之后的页面没法加载css、js等静态文件

    1.是否被拦截,这个在Web.xml配置中servlet拦截是“/”,如果是则 a.使用spring MVC 的静态资源文件 <!-- 静态文件访问,主要是针对DispatcherServlet ...