题目简述:

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?

解题思路:

这个是最简单的动态规划问题,我们考虑爬上n阶有几种情况?既然只能一次走一步或者是两步,那么要到第n阶的种数要不然是从第n-1阶走一步上去,要不然是从第n-2阶走两步上去。写成数学式子就是:\(a[n]=a[n-1]+a[n-2]\)

class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n == 1:
return 1
if n == 2:
return 2
a = [None] * n
a[0] = 1
a[1] = 2
for i in range(2,n):
a[i] = a[i-1] + a[i-2]
return a[n-1]

【leetcode】Climbing Stairs的更多相关文章

  1. 【leetcode】Climbing Stairs (easy)

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

  2. 【题解】【DP】【Leetcode】Climbing Stairs

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

  3. 【Leetcode】【Easy】Climbing Stairs

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

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. PostgreSQL Apt Repository

    PostgreSQL Apt Repository If the version included in your version of Ubuntu is not the one you want, ...

  2. 自定义Docker容器的 hostname

    自定义Docker容器的 hostname   作者: John Deng 原创内容,欢迎传播,请注明出处:http://www.cnblogs.com/johnd/p/set-docker-host ...

  3. oracle---触发器总结

    一.触发器简介 触发器的定义就是说某个条件成立的时候,触发器里面所定义的语句就会被自动的执行.因此触发器不需要人为的去调用,也不能调用.然后,触发器的触发条件其实在你定义的时候就已经设定好了.这里面需 ...

  4. ionic browser+ios头部高度显示问题

    ionic项目在使用ionic build browser或者打包ios时如果设置头部高度 方法如下 .bar-header { padding:; height:; } .scroll-conten ...

  5. Level Of Management Protocols - SNMP Tutorial

    30.2 The Level Of Management Protocols Originally, many wide area networks included management proto ...

  6. 出现could not find developer disk image解决办法和不受信任的开发者

    真机测试问题 最近一直遇到这样的问题,很是让人心烦,但是还是要自己解决的,我也是从网上查了很多这样的解决办法,都没有成功,所以今天我要把自己的总结的方法和大家分享一下. iOS测试当中的问题 iOS ...

  7. sql中的inner join ,left join ,right join

    左连接LEFT JOIN, 也就是说,左外连接的含义是限制连接关键字右端的表中的数据必须满足连接条件,而不关左端的表中的数据是否满足连接条件,均输出左端表中的内容.不满足连接条件的 ,连接字段栏位将对 ...

  8. Web jquery表格组件 JQGrid 的使用 - 11.问题研究

    系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...

  9. eclipse安装Eclipse Memory Analyzer插件

    在Install New software中输入 http://archive.eclipse.org/mat/1.2/update-site/ 然后选择Memory Analyzer for Ecl ...

  10. CentOS添加163源

    1.备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...