• LeetCode:70. 爬楼梯


    假设你正在爬楼梯。需要 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 阶

    class Solution:
        def climbStairs(self, n: int) -> int:
            prev,current = 0,1
            # n = 1 :  1
            # n = 2 :  2
            # n = 3 :  3 = 2 + 1
            # n = 4 :  5 = 3 + 2
            #........
            for i in range(n):
                prev,current = current,prev+current
            return current

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/climbing-stairs
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 相关阅读:
    package.json 笔记
    TypeScript 笔记
    RxJS 笔记
    angular 使用Redux
    ngrx 笔记
    Node 的使用
    imoocLinux环境变量配置文件笔记
    imooc正则表达式学习笔记
    js定时器和linux命令locate
    linux修改PATH环境
  • 原文地址:https://www.cnblogs.com/Halo-zyh-Go/p/12408163.html
Copyright © 2020-2023  润新知