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?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step Recursion with memory:
M(n)=M(n-1)+M(n-2)
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
a=[0]*n
def findM(n):
if n==1:
a[0]=1
return 1
elif n==2:
a[1]=2
return 2
else:
if a[n-1-1]==0:
a[n-1-1]=findM(n-1)
if a[n-2-1]==0:
a[n-2-1]=findM(n-2)
a[n-1]=a[n-1-1]+a[n-2-1]
return a[n-1]
findM(n)
return a[n-1]

  

[LeetCode&Python] Problem 70. Climbing Stairs的更多相关文章

  1. 【leetcode❤python】70. Climbing Stairs

    #Method1:动态规划##当有n个台阶时,可供选择的走法可以分两类:###1,先跨一阶再跨完剩下n-1阶:###2,先跨2阶再跨完剩下n-2阶.###所以n阶的不同走法的数目是n-1阶和n-2阶的 ...

  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] 70. 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之70. Climbing Stairs Easy

    Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...

  5. 42. leetcode 70. Climbing Stairs

    70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...

  6. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  7. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  8. 377. Combination Sum IV 70. Climbing Stairs

    back function (return number) remember the structure class Solution { int res = 0; //List<List< ...

  9. 刷题70. Climbing Stairs

    一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...

随机推荐

  1. 【Angular 5】数据绑定、事件绑定和双向绑定

    本文为Angular5的学习笔记,IDE使用Visual Studio Code,内容是关于数据绑定,包括Property Binding.Class Binding.Style Binding. 在 ...

  2. 生成git,ssh的key

    git clone ssh 代码: 报错: Warning: Permanently added 'gitee.com,120.55.226.24' (ECDSA) to the list of kn ...

  3. HTML5-2

    1 html5新增表单元素 <form> <!-- 数字 还有箭头,可以加减数字 并且可以设置最大最小值 --> <input type="number&quo ...

  4. 软工作业PSP与单元测试训练

    任务说明(二选一): 一.实现模块判断传入的身份证号码的正确性: 二.实现模块判断传入的电子邮箱账号的正确性: 选择任务二: 实现要求: 一.实现功能模块: 1. 判断邮箱地址是否为空: 2. 判断邮 ...

  5. BPM与OA区别

    核心差异: OA解决的是流程有无问题 BPM解决的是流程更好更优的问题 主要差异如下: 1.BPM有更好的广度跟深度 这里的广度是指应用场景的广度. BPM一般都会以端到端的方式衔接企业运营过程的上下 ...

  6. spring boot读取classpath下的json文件

    import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resour ...

  7. Excel--------实用功能(数据对比)

    --excel数据在sql中查询展示出来 SELECT * FROM (SELECT '101001' as code ,'上海宝山站' as name union allSELECT '102083 ...

  8. javascript数据加减问题

    需要parseInt把获取到的html(),text()的值转换为数字型,然后进行加减乘除操作就可以了:

  9. [Deep Learning] mini-batch

    转自 http://hp.stuhome.net/index.php/2016/09/20/tensorflow_batch_minibatch/ 深度学习的优化算法,说白了就是梯度下降.每次的参数更 ...

  10. selenium+python启动浏览器出错,安装浏览器驱动

    WebDriver 支持 Firefox (FirefoxDriver).IE (InternetExplorerDriver).Opera (OperaDriver) 和 Chrome (Chrom ...