LetCode 70. Climbing Stairs--动态规划-爬梯子--递归等解法
LeetCode 动态规划(Dynamic programming)系列题目:LeetCode 动态规划(Dynamic programming)系列题目
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
爬梯子,动态规划即可。
Python代码如下:
class Solution:def climbStairs(self, n: int) -> int:t=[1,2]for i in range(2,n):t.append(t[i-1]+t[i-2])return t[n-1]
java解法:
class Solution {public int climbStairs(int n) {int []dp=new int[n+1];if(n==1){return 1;}dp[1]=1;dp[2]=2;for (int i=3;i<=n;i++){dp[i]=dp[i-1]+dp[i-2];}return dp[n];}
}
python如果直接使用递归的话,会爆内存,使用缓存可以解决这个问题:
import functoolsclass Solution:@functools.lru_cache()def climbStairs(self, n: int) -> int:return self.climbStairs(n - 1) + self.climbStairs(n - 2) if n > 2 else 2 if n == 2 else 1
可以直接使用斐波那契公式,Java代码如下:
public class Solution {public int climbStairs(int n) {double sqrt5=Math.sqrt(5);double fibn=Math.pow((1+sqrt5)/2,n+1)-Math.pow((1-sqrt5)/2,n+1);return (int)(fibn/sqrt5);}
}
LetCode 70. Climbing Stairs--动态规划-爬梯子--递归等解法相关推荐
- 70. Climbing Stairs【leetcode】递归,动态规划,java,算法
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
LeetCode 70. Climbing Stairs 这是一道利用斐波那切数列求解的题目.求斐波那切数列有比较经典的4种方法 (1)递归法:复杂度太高 (2)迭代法:时间复杂度为O(n)O(n)O ...
- [勇者闯LeetCode] 70. Climbing Stairs
[勇者闯LeetCode] 70. Climbing Stairs Description You are climbing a stair case. It takes n steps to rea ...
- 70. Climbing Stairs
70. Climbing Stairs 1. 题目 You are climbing a stair case. It takes n steps to reach to the top. Each ...
- [LeetCode]70.Climbing Stairs
[题目] You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...
- leetcode python3 简单题70. Climbing Stairs
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...
- Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- [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
Problem Definition: You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
最新文章
- 【深度学习】数形结合的图像处理(文末介绍了一种新型网络)
- Ember.js系列文章
- 【计算理论】非确定性有限自动机 ( 计算过程 | 计算树 | 确定可接受字符串 | 设计非确定性有限自动机 | 空字符 )
- 5G在工业互联网应用的机遇与挑战
- 第六十六期:软件架构之道的一次感悟
- 【软件质量】软件设计要考虑性能
- linux 改变 asm磁盘组 权限,DBCA时出现ASM磁盘组权限问题ORA-27303
- 下血本了!京东宣布未来三年向湖北投资,助力经济恢复
- python期末复习卷_【期末复习卷A】六年级科学上册期末复习试题
- php点击下拉框触发数据汇总,easyUI下拉列表点击事件使用方法
- Linux常用命令 创建文本 清屏操作
- Qt Plain C++ Project error: undefined reference to `pthread_create'
- java 读取文件的大小_java读取文件大小
- matlab两张图片合成一张_MATLAB|多个Figure图按子图合成一个Figure
- 计算机cpu操作ppt,CPU基础知识PPT课件
- 浏览器 之 无头浏览器
- 淘宝最新开店卖家店铺采集工具能采集同行信誉等级高的店铺吗
- 雅思和托福的区别(转)
- 电位移矢量法向向量连续性证明
- 安防巡逻机器人在不同应用场景下的作用是什么?
热门文章
- Jupyter Notebook实现直接调用R
- Shiny平台构建与R包开发(一)——ui布局
- dis的前缀单词有哪些_英语单词词根.词缀II 5个最高频英语前缀 必背
- 安装batocera-linux教程_利用Grub2Win安装Batocera+Windows7双系统甚至多系统实战教程
- MPB:利用无菌植物和可培养细菌体系研究根系微生物组功能
- “中药资源创新院士团队”诚聘结构、合成和计算生物学博士后5-10名
- 单细胞转录组单飞第二期开课啦!!
- MetaPhlAn2-增强版宏基因组分类谱工具-一条命令获得宏基因组物种组成
- R语言KMeans聚类分析确定最优聚类簇数实战:间隙统计Gap Statistic(确定最优聚类簇数)
- pyinstaller打包生成的exe文件并使用python终止后台的exe程序运行