leetcode python3 简单题70. Climbing Stairs
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第七十题
(1)题目
英文:
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.
中文:
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/climbing-stairs
(2)解法
① 递归+记忆,递归的思想就是将大问题换成最简单的小问题,记忆是为了节省重复计算的时间
(耗时:48ms,内存:13.8M)
class Solution:def climbStairs(self, n: int) -> int:memo = [0] * (n+1)memo[0] = memo[1] = 1def climb(n):if memo[n] > 0:return memo[n]else:method = climb(n-1) + climb(n-2)memo[n] = methodreturn methodreturn climb(n)
注意:
1.这里之所以要创建n+1个0是因为想让阶梯个数与memo的indx是一一对应的。
② 动态规划:从最少的阶梯往上找,所以思想与递归是反着来的
(耗时:48ms,内存:13.6M)
class Solution:def climbStairs(self, n: int) -> int:memo = [0] * (n+1)memo[0] = memo[1] = 1if n == 1:return 1else:for i in range(2, n+1):memo[i] = memo[i-1] + memo[i-2]return memo[n]
③ 简单的想法:先从最简单的一个阶梯,二个阶梯开始,后面的都是有规律的:
第n个阶梯的方法数=第n-1个阶梯的方法数+第n-2个阶梯的方法数。这里的-1,-2是由题目中一次只能爬一个阶梯或者两个阶梯决定的。
(耗时:44ms,内存:13.4M)
class Solution:def climbStairs(self, n: int) -> int:first = 1second = 2method = 0for i in range(2, n):method = first + second first = second second = method return max(method, n)
注意:
1.最后一句max是用于n=1,2时也能输出正确值,因为range(2,n)只有当n>=3时才会有值出现。
leetcode python3 简单题70. Climbing Stairs相关推荐
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题69. Sqrt(x)
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十九题 (1)题目 英文: Implement int sqrt(int x). ...
- leetcode python3 简单题53. Maximum Subarray
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...
- leetcode python3 简单题58. Length of Last Word
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十八题 (1)题目 英文: Given a string s consists ...
- leetcode python3 简单题14. Longest Common Prefix
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十四题 (1)题目 英文: Write a function to find th ...
- leetcode python3 简单题234. Palindrome Linked List
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十四题 (1)题目 英文: Given a singly linked li ...
- leetcode python3 简单题232. Implement Queue using Stacks
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十二题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题231. Power of Two
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十一题 (1)题目 英文: Given an integer, write ...
- leetcode python3 简单题226. Invert Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十六题 (1)题目 英文: Invert a binary tree. 中文 ...
最新文章
- C中printf函数的实现原理
- php 简单路由实现
- JQuery学习笔记 [Ajax] (6-2)
- 还在埋头写论文?知网检索的这些小技巧让你有如神助!
- 深度学习100例 | 第28天:水果的识别与分类(准确率99.9%)
- c语言计算除法一位小数点,高精度除法小数点位数
- 在将varchar值id转换为int时失败_Python—CAN报文转换工具教程
- python输入三个商品_用python3采集shopify站点商品
- I/0口输入输出实验 流水灯程序 P0、P1、P2、P3口作为输出口,连接八只发光二极管,编写程序,使发光二极管从左至右循环点亮。
- html 循环_一个不被程序员认为是编程语言的语言——HTML,你怎么看?
- [Istioc]Istio部署sock-shop时rabbitmq出现CrashLoopBackOff
- Android中的消息通知Toast和Notification
- 微信WAP H5支付功能实现
- 如何在本地运行jar文件
- (6.0系统)安卓神器XPOSED框架无需ROOT安装指南
- 【工作小技巧】cmd 批量移动文件
- 基于群智能算法的函数最值优化问题
- 2017 Material design 第三章第三节《图像》
- RatingBar详解
- 绿盟科技网络安全威胁周报2017.15 建议关注方程式组织泄漏大量针对Windows攻击工具...
热门文章
- matlab百分制到5分制的转换,绩点换算百分制(绩点5分制百分对照表)
- 3d 自动生成物体_CVPR2020论文介绍: 3D 目标检测高效算法
- 挑战杯获奖作品_我校学生参加河北省“挑战杯”竞赛获奖作品展示二
- tensorflow sigmoid 如何计算训练数据的正确率_“来自蒙娜丽莎的凝视”— 结合 TensorFlow.js 和深度学习实现...
- 静态类对象指针需要delete吗 vc_C/C++真的有那么难学吗?其实不然,无非是你没有找对方法罢了...
- Python内置函数之 range()
- 突击计划——求整数中的较大者
- 深度学习之OCR相关经验记录
- J.U.C - AQS
- win7打开chm文件问题解决之道