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.

public int climbStairs(int n) {
    // base cases
    if(n <= 0) return 0;
    if(n == 1) return 1;
    if(n == 2) return 2;
    
    int one_step_before = 2;
    int two_steps_before = 1;
    int all_ways = 0;
    
    for(int i=2; i<n; i++){
    	all_ways = one_step_before + two_steps_before;
    	two_steps_before = one_step_before;
        one_step_before = all_ways;
    }
    return all_ways;
}

Here are the steps to get the solution incrementally.

  • Base cases:
    if n <= 0, then the number of ways should be zero.
    if n == 1, then there is only way to climb the stair.
    if n == 2, then there are two ways to climb the stairs. One solution is one step by another; the other one is two steps at one time.

  • The key intuition to solve the problem is that given a number of stairs n, if we know the number ways to get to the points [n-1] and [n-2] respectively, denoted as n1 and n2 , then the total ways to get to the point [n] is n1 + n2. Because from the [n-1] point, we can take one single step to reach [n]. And from the [n-2] point, we could take two steps to get there. There is NO overlapping between these two solution sets, because we differ in the final step.


<think>好的,我需要帮助用户查找LeetCode第70题的解决方案和题目描述。首先,我应该确认用户的问题是关于LeetCode的爬楼梯问题,即Climbing Stairs。根据用户提供的引用信息,引用[1]、[3]和[5]提到了这个问题,尤其是引用[3]给出了一个具体的例子,输入3时输出3,说明有3种方法爬到顶部。 接下来,我需要整理题目描述。题目通常要求给定n阶楼梯,每次可以爬1或2步,返回不同的方法数。根据引用[3]的例子,当n=3时,答案是3,这符合斐波那契数列的规律,因为f(3)=f(2)+f(1)=2+1=3。 然后,解决方案部分。用户提供的引用[1]中提到了一种优化方法,将时间复杂度从$O(n^2)$降低到$O(n)$,可能是指动态规划的方法。动态规划是解决这个问题的常用方法,因为每一步的方法数等于前一步和前两步的方法数之和。此外,还可以使用递归或记忆化搜索,但动态规划在时间上更高效。 接下来,我需要检查是否有其他方法,比如矩阵快速幂或斐波那契公式,但根据用户提供的引用,可能只需要提到动态规划和递归两种方法。同时,引用[5]中提到的Python链表操作可能不相关,但需要注意Python的自动内存管理,这可能影响代码的写法,比如不需要手动删除节点。 然后,需要确保代码示例正确。动态规划的Python实现应该使用循环,从1到n逐步计算。递归方法可能会有栈溢出的问题,但作为示例可以提及。根据引用[1]的代码示例,用户可能希望看到简洁的代码,如使用变量交换来优化空间复杂度到$O(1)$。 最后,生成相关问题。用户可能对时间复杂度、空间复杂度优化、不同解法比较感兴趣,比如动态规划与递归的区别,或者如何用数学方法解决。同时,应用场景可能涉及斐波那契数列的其他应用。 需要确保回答结构清晰,分点说明题目描述、解决方案和代码示例,并正确引用用户提供的资料。特别是引用[1]和[3]应出现在适当的位置,比如动态规划的解释和示例部分。</think>### LeetCode Problem 70. Climbing Stairs 题解与题目描述 #### 题目描述 给定一个整数$n$表示楼梯阶数,每次可以爬1或2个台阶,求有多少种不同的方法可以到达楼顶[^3]。 示例: 当$n=3$时,共有3种方法: 1. 1步 + 1步 + 1步 2. 1步 + 2步 3. 2步 + 1步 --- #### 解决方案 该问题本质是斐波那契数列的变种,递推公式为: $$f(n) = f(n-1) + f(n-2)$$ 其中$f(1)=1$,$f(2)=2$。 ##### 方法1:动态规划(优化空间) 通过滚动变量优化空间复杂度至$O(1)$,时间复杂度$O(n)$: ```python def climbStairs(n: int) -> int: if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b ``` ##### 方法2:递归(带记忆化) 通过缓存中间结果减少重复计算,时间复杂度$O(n)$: ```python from functools import lru_cache @lru_cache(maxsize=None) def climbStairs(n: int) -> int: if n <= 2: return n return climbStairs(n-1) + climbStairs(n-2) ``` --- #### 复杂度分析 | 方法 | 时间复杂度 | 空间复杂度 | 适用场景 | |------------|------------|------------|------------------------| | 动态规划 | $O(n)$ | $O(1)$ | 大规模输入(推荐) | | 递归 | $O(n)$ | $O(n)$ | 小规模输入或教学示例 | --- 相关问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值