LeetCode | 0070. Climbing Stairs爬楼梯【Python】

时间:2020-03-27
本文章向大家介绍LeetCode | 0070. Climbing Stairs爬楼梯【Python】,主要包括LeetCode | 0070. Climbing Stairs爬楼梯【Python】使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

LeetCode 0070. Climbing Stairs爬楼梯【Easy】【Python】【动态规划】

Problem

LeetCode

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

问题

力扣

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意: 给定 n 是一个正整数。

示例 1:

输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。

1.  1 阶 + 1 阶
2.  2 阶

示例 2:

输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。

1.  1 阶 + 1 阶 + 1 阶
2.  1 阶 + 2 阶
3.  2 阶 + 1 阶

思路

动态规划

初始条件和斐波那契数列有点区别:dp_0 = 1,dp_1 = 1。

递推公式:fib(n) = fib(n - 1) + fib(n - 2)

时间复杂度: O(n)
空间复杂度: O(1)

Python3代码
class Solution:
    def climbStairs(self, n: int) -> int:
        # 初始条件和斐波那契数列有区别
        dp_0, dp_1 = 1, 1
        for _ in range(n):
            dp_0, dp_1 = dp_1, dp_0 + dp_1
        return dp_0

GitHub链接

Python

原文地址:https://www.cnblogs.com/wonz/p/12584070.html