[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 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的更多相关文章
- 【leetcode❤python】70. Climbing Stairs
#Method1:动态规划##当有n个台阶时,可供选择的走法可以分两类:###1,先跨一阶再跨完剩下n-1阶:###2,先跨2阶再跨完剩下n-2阶.###所以n阶的不同走法的数目是n-1阶和n-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. ...
- [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 ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 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 ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 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 ...
- 377. Combination Sum IV 70. Climbing Stairs
back function (return number) remember the structure class Solution { int res = 0; //List<List< ...
- 刷题70. Climbing Stairs
一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
随机推荐
- NullReferenceException 的可恨之处
通常我们在取数据库记录或者字段时,获取不存在的值时,会出现 NullReferenceException 如果根据某个键值去LoadById, 我们通常会检查一下这个键值是否在数据库里存在. 但如果I ...
- vmware install win8 and server2012 problem
Environment: vmware workstation 9 and win7 64bit. Problem: when i want to install server2012r2 and w ...
- learning makefile string example
- UVa LA 4094 WonderTeam 构造 难度: 1
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- work2:贪吃蛇
学号:2017*****7219 姓名:邰嘉琛我的码云贪吃蛇项目仓库:https://gitee.com/tjc666/sesnake/tree/master2) 给出你的各项任务完成时间估算与实际消 ...
- js dictionary
转载的 1.dictionary例子 <script type="text/javascript" language="javascript"> v ...
- 七、Linux的权限命令
1. 文件权限 r:对文件是指可读取内容 对目录是可以ls w:对文件是指可修改文件内容,对目录 是指可以在其中创建或删除子节点(目录或文件) x:对文件是指是否可以运行这个文件,对目录是指是否可以c ...
- java切割音频文件
工具: 一个jar包即可:jave-1.0.2.jar 可以切割wav格式的音频文件 完整工程目录 就一个jar包,一个main类 代码: package com.zit; import java.i ...
- js-notebook
注意DOM和BOM的区别,ECMAScript只针对js的语法核心,实际大部分浏览器里的js = ECMAScript + DOM + BOM, 而nodejs里就只包括core js 隐性的toSt ...
- 九州动态ip的特色
九州代理是一款高覆盖的换ip软件,范围可覆盖全国160多个城市.软件可用于游戏试玩.游戏挂机.营销.优化.文档分享.管理.问答推广.数据采集.点赞.增效回访.用户注册等.“九州代理”仅提供国内网络节点 ...