题目简述:

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. 教你一招:Excel中使用vlookup函数查询序列所对应的值

    以一个简单的例子做示范,列数相对较少,看起来也比较清楚:在奥运会或其他比赛上我们可以看到各个国家的奖牌数的变化:那么我们如何查询国家对应的总奖牌数. 我们用到的函数是vlookup,它是一个纵向查询函 ...

  2. 解决:Error: JAVA_HOME is not defined correctly

    问题重现: Error: JAVA_HOME is not defined correctly. We cannot execute :/usr/lib/jvm/java-7-oracle 问题分析: ...

  3. Photon服务器进阶&一个新游戏的出产(一)

    许久没写了,今日闲来无事随性一些,总结一下最近的游戏经历(写游戏) Photon服务器的进阶~ 距离上一篇估计也过去一个多月了,对于Photon真是又爱又恨. 爱着它的书写方便. 恨着它的教程稀少总走 ...

  4. CentOS6.5下Redis安装与配置

    http://blog.csdn.net/ludonqin/article/details/47211109

  5. kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)

    问题导读: 1.zookeeper在kafka的作用是什么? 2.kafka中几乎不允许对消息进行"随机读写"的原因是什么? 3.kafka集群consumer和producer状 ...

  6. Win10---------专区

    待完善中---------------------------------- -----------------------------------------The End------------- ...

  7. 如何学习Oracle

    如何学习Oracle?分清几个概念是关键     经常有一些Oracle的初学者问到以下几个问题,这里集中解答一下,希望对大家有帮助.   1.如果有一定的数据库基础,知道SQL是怎么回事,即使写不出 ...

  8. lucene大索引文件分布式存储方案

    这几天实现了个Lucene分布式检索的模块,采用的分布式方案是将数据分块,分别生成N个索引文件,放到N个节点上运行.检索时,对每一个节点发出查询请求,将N个节点返回的结果归并,然后生成一个新的结果.如 ...

  9. Html:form表单

    1:onsubmit 事件:会在表单中的确认按钮被点击时发生. <form action="" method="post" name="form ...

  10. 使用excel计算指数平滑和移动平均

      指数平滑法 原数数据如下: 点击数据——数据分析 选择指数平滑 最一次平滑 由于我们选择的区域是B1:B22,第一个单元格“钢产量”,被当做标志,所以我们应该勾选标志.当我们勾选了标志后,列中的第 ...