作者: 负雪明烛
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)的更多相关文章

  1. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. 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 ...

  4. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  5. 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 ...

  6. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  7. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  8. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  9. [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 ...

随机推荐

  1. C语言 自定义函数按行读入文件

    在之前的博客中 https://www.cnblogs.com/mmtinfo/p/13036039.html 读取一行的getline()函数是GNU 的扩展函数. 这里用自定义函数实现这个功能,从 ...

  2. 苹果ios通过描述文件获取udid

    苹果ios通过描述文件获取udid 需要准备的东西 1,安装描述文件只支持https的回调地址,所以需要申请https域名 2,描述文件签名,不安装也可,只要能接受红色的字 步骤: 1,准备xml文件 ...

  3. 通过yum安装 memcache

    . 通过yum安装 复制代码代码如下: yum -y install memcached#安装完成后执行:memcached -h#出现memcached帮助信息说明安装成功 2. 加入启动服务 复制 ...

  4. 日常Java 2021/10/12

    封装 在面向对象程式设计方法中,封装是指-种将抽象性函式接口的实现细节部分包装.隐藏起来的方法 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问 要访问该类的代码和数据,必 ...

  5. A Child's History of England.48

    A few could not resolve to do this, but the greater part complied. They made a blazing heap of all t ...

  6. Celery进阶

    Celery进阶 在你的应用中使用Celery 我们的项目 proj/__init__.py   /celery.py   /tasks.py 1 # celery.py 2 from celery ...

  7. 大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户

    1. 读取数据库的形式创建DataFrame DataFrameFromJDBC object DataFrameFromJDBC { def main(args: Array[String]): U ...

  8. openwrt装载固件

    方法1. 确定串口号以后(在设备管理器可以查看) 打开SecureCRT软件,选择串口,设置合适的波特率(我用的115200),然后快速连接, 板子通电启动,在启动的时候会提示按任意键中断,这时按下任 ...

  9. 转 android design library提供的TabLayout的用法

    原文出处:http://chenfuduo.me/2015/07/30/TabLayout-of-design-support-library/ 在开发中,我们常常需要ViewPager结合Fragm ...

  10. Linux(CentOS)升级gcc版本

    本人使用的是CentOS 6.2 64位系统,由于在安装系统的时候并没有勾选安装gcc编译器,因此需要自行安装gcc编译器. 系统信息查看命令: cat /etc/redhat-release 使用y ...