[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! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
随机推荐
- 8、TypeScript-解构赋值
1.数组的解构赋值 2.对象的解构赋值 注意:在浏览器环境中,windows本身有一个成员name,所以要重新,语法为 属性名:重命名 3.函数的解构赋值
- react native练习
import React, { Component } from 'react' import { Platform, StyleSheet, Text, View,Image ,FlatList} ...
- angular6 input节流
一直以为 pipe(debounceTime(1000), distinctUntilChanged()) 不起作用 原因:使用方法错误 <input type="text&qu ...
- python模块part1
一.时间模块 1.时间表示形式 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串:(1)时间戳(timestamp) :通常来说,时间戳表示的是 ...
- HTCVIVE定位器更新之后,定位器指示灯不亮,重置基站固件操作指南。
HTCVIVE定位器更新之后,定位器指示灯不亮,固件修复指南 建议您重置基站固件,操作如下:请您使用手机来拍照运行中基站的“激光发射器”面板,并且数一下是否有17颗LED灯,如果没有17颗,则基本可以 ...
- vscode 创建.net core项目初体验
微软的virtual studio编辑器那是宇宙第一大编辑器,可惜就是太笨重,遇到性能差一些的电脑设备,简直无法快速的编辑项目. 而vs code编辑器轻便易用,想要编辑哪种项目,只需扩展插件就OK, ...
- 第一章 Python基本语法
寒假不能荒废,终于静下心来认真地开始学习Python,在这里与大家分享一下所学知识,希望能对像我这样的小白有所帮助,如有错误之处,谢大佬不吝赐教!! 编程语言包括机器语言.汇编语言.高级语言.超 ...
- idea安装proto插件后不能识别.proto文件解决方案
just had a try and it worked well. Could you please check "File Types" in IDEA? Open &quo ...
- MFC动态创建对话框中的按钮控件并创建其响应消息
转自:http://www.cnblogs.com/huhu0013/p/4626686.html 动态按钮(多个)的创建: 1.在类中声明并定义按钮控件的ID #define IDC_D_BTN 1 ...
- C#异常:未将对象引用设置到对象的实例。
异常:未将对象引用设置到对象的实例. 一般是定义的变量或者数组等,没有赋初始值. 赋初始值后问题解决.