【LeetCode】70. Climbing Stairs 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/climbing-stairs/
Total Accepted: 106510 Total Submissions: 290041 Difficulty: Easy
题目大意
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
题目大意
有多少种不同的爬楼梯到达顶部的方式,每次可以走一个台阶或者两个台阶。
解题方法
注意题目中的意思是,有多少种方法,也就是说加入三个台阶,1,2与2,1是不同的。
递归
用费布拉奇数列的方法。
为什么呢?因为每次增加一个台阶可以认为是在前面那个解法中任意的一步增加一步。额,我也说不明白。
写出来前面几个数值就能看出来。
1 --> 1
2 --> 2
3 --> 3
4 --> 5
……
解法:
public class Solution {
public int climbStairs(int n) {
if(n==1) return 1;
if(n==2) return 2;
return climbStairs(n-1)+climbStairs(n-2);
}
}
但是!超时!因为这个方法太慢了,循环次数太多。
记忆化搜索
上面超时的原因主要是同样的n被求了很多遍。如果使用记忆化,那么就不用重复求解。
所以使用一个字典用来保存已经求得的结果就好了。
class Solution(object):
def __init__(self):
self.memo = dict()
self.memo[0] = 1
self.memo[1] = 1
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n in self.memo:
return self.memo[n]
steps = self.climbStairs(n - 1) + self.climbStairs(n - 2)
self.memo[n] = steps
return steps
动态规划
动态规划,需要建立一个数组,然后从头开始遍历,在本题中每个位置的结果就是前两个数相加。看最后一个数值就好了。
这里需要注意的地方是初始化的大小是n+1,因为保存了0的位置步数是1,要求n的步数,所以总的是N+1个状态。
public class Solution {
public int climbStairs(int n) {
int[] counts=new int[n+1];
counts[0]=1;
counts[1]=1;
for(int i=2;i<=n;i++){
counts[i]=counts[i-1]+counts[i-2];
}
return counts[n];
}
}
AC:0ms
DP的Python解法如下:
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[-1]
空间压缩DP
我们看到DP的每个状态之和前面两个状态有关,所以可以使用空间压缩,只需要使用三个变量即可,也可以使用大小为3的数组进行循环利用。
Java解法如下:
public class Solution {
public int climbStairs(int n) {
int[] counts=new int[3];
counts[0]=1;
counts[1]=1;
for(int i=2;i<=n;i++){
counts[i%3]=counts[(i-1)%3]+counts[(i-2)%3];
}
return counts[n%3];
}
}
AC:0ms
Python解法如下:
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1
first, second = 1, 2
for i in range(3, n + 1):
third = first + second
first = second
second = third
return second
日期
2016/5/1 16:19:44
2018 年 11 月 19 日 —— 周一又开始了
【LeetCode】70. Climbing Stairs 解题报告(Java & Python)的更多相关文章
- 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 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 ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- [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 ...
随机推荐
- Docker网络设置及文件挂载
网络设置–net=bridge- 默认选项,用网桥的方式来连接docker容器.–net=host- docker跳过配置容器的独立网络栈.–net=container:NAME_or_ID- 告诉d ...
- c++基础知识03
1.嵌套循环案例--九九乘法表 int main() { //利用嵌套循环乘法口诀表 for (int n = 1; n <= 9; n++) { for (int m = 1; m <= ...
- 这份github上被14万人点赞的Java教程太强了
前几天有个小伙伴加我之后问了下面的这个问题.我看到后是一脸懵逼的状态,jcombobox?实话说,我已经完全忘了在Java中还有这么个东西. 在网上一番搜索后,才发现原来它是 swing 中的下拉列表 ...
- 学习java 7.28
学习内容: Applet Applet一般称为小应用程序,Java Applet就是用Java语言编写的这样的一些小应用程序,它们可以通过嵌入到Web页面或者其他特定的容器中来运行,也可以通过Java ...
- 面对大规模 K8s 集群,这款诊断利器必须要“粉一波”!
作者|段超 来源|尔达 Erda 公众号 背景 我们是一家做商业软件的公司,从一开始我们就把软件交付流程做的非常标准且简单,所有的软件都是基于我们的企业数字化平台 Erda(现已开源)来交付,底层基于 ...
- 纯CSS圆环与圆
1. 两个标签的嵌套: <div class="element1"> <div class="child1"></div> ...
- int是几位;short是几位;long是几位 负数怎么表示
其实可以直接通过stm32的仿真看到结果:(这里是我用keil进行的测试,不知道这种方法是否准确) 从上面看, char是8位 short是4*4=16位 int是8*4=32位 long是8* ...
- [学习总结]8、android 自定义控件 使用declare-styleable进行配置属性(源码角度)
declare-styleable:declare-styleable是给自定义控件添加自定义属性用的. 官方的相关内部控件的配置属性文档:http://developer.android.com/r ...
- Java 使用slf4j记录日志
引入依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12< ...
- 【Linux】【Shell】【Basic】数组
1. 数组: 变量:存储单个元素的内存空间: 数组:存储多个元素的连续的内存空间: 数组名:整个数组只有一个名字: 数组 ...