【陪伴式刷题】Day 34|动态规划|509.斐波那契数列(Fibnacci Number)和70.爬楼梯(Climbing Stairs)

刷题顺序按照代码随想录建议

三种方法实现经典的509题斐波那契数列,脱掉场景的外套,70题的本质也是一个斐波那契数列,这两道题非常的像,所以放在一起

题目描述:509

英文版描述

The Fibonacci numbers , commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1

F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

Example 1:

Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: n = 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: n = 4 Output: 3 Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Constraints:

  • 0 <= n <= 30

英文版地址

leetcode.com/problems/mi...

中文版描述

斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:

F(0) = 0,F(1) = 1

F(n) = F(n - 1) + F(n - 2),其中 n > 1

给定 n ,请计算 F(n) 。

示例 1:

输入: n = 2 输出: 1 解释: F(2) = F(1) + F(0) = 1 + 0 = 1

示例 2:

输入: n = 3 输出: 2 解释: F(3) = F(2) + F(1) = 1 + 1 = 2

示例 3:

输入: n = 4 输出: 3 解释: F(4) = F(3) + F(2) = 2 + 1 = 3

提示:

  • 0 <= n <= 30

中文版地址

leetcode.cn/problems/fi...

解题方法

法1

kotlin 复制代码
class Solution {
    public int fib(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }
}

复杂度分析

  • 时间复杂度:O(2^n)
  • 空间复杂度:O(n),递归栈深度

法2

ini 复制代码
class Solution {
    public int fib(int n) {
        if (n < 2) return n;
        int a = 0, b = 1, c = 0;
        for (int i = 1; i < n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
}

复杂度分析

  • 时间复杂度:O(n)
  • 空间复杂度:O(1), 几个标志变量使用常数大小的额外空间计算

法3

ini 复制代码
class Solution {
    public int fib(int n) {
        if (n <= 1) return n;             
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int index = 2; index <= n; index++){
            dp[index] = dp[index - 1] + dp[index - 2];
        }
        return dp[n];
    }
}

复杂度分析

  • 时间复杂度:O(n)
  • 空间复杂度:O(1), 几个标志变量使用常数大小的额外空间计算

题目描述:70

英文版描述

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Example 1:

  1. Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1 step + 1 step 2 steps

Example 2:

  1. Input: n = 3 Output: 3 Explanation: There are three ways to climb to the top. 1 step + 1 step + 1 step 1 step + 2 steps 2 steps + 1 step

Constraints:

  • 1 <= n <= 45

英文版地址

leetcode.com/problems/cl...

中文版描述

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

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

示例 1:

  1. 输入: n = 2 输出: 2 解释: 有两种方法可以爬到楼顶。 1 阶 + 1 阶 2 阶

示例 2:

  1. 输入: n = 3 输出: 3 解释: 有三种方法可以爬到楼顶。 1 阶 + 1 阶 + 1 阶 1 阶 + 2 阶 2 阶 + 1 阶

提示:

  • 1 <= n <= 45

中文版地址

leetcode.cn/problems/cl...

解题方法

用数组记录方法数

ini 复制代码
class Solution {
    public int climbStairs(int n) {
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[0] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }
}

复杂度分析

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

用变量记录代替数组

css 复制代码
// 用变量记录代替数组
class Solution {
    public int climbStairs(int n) {
        if(n <= 2) return n;
        int a = 1, b = 2, sum = 0;

        for(int i = 3; i <= n; i++){
            sum = a + b;  // f(i - 1) + f(i - 2)
            a = b;        // 记录f(i - 1),即下一轮的f(i - 2)
            b = sum;      // 记录f(i),即下一轮的f(i - 1)
        }
        return b;
    }
}

复杂度分析

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
相关推荐
小张小张爱学习7 分钟前
Apache Dubbo 3 + Spring Boot 3 多模块 Maven 项目,演示基于 Zookeeper 注册中心的 RPC 调用
java·dubbo
阿pin36 分钟前
Java随笔-ConcurrentHashMap
java·开发语言·哈希算法
C+-C资深大佬1 小时前
Java 变量:从入门到精通
java·开发语言·python
Flittly1 小时前
【日常小问】Docker 部署 Nacos 2.4.3,Dubbo 3.3 连接 Config Service 失败排查
java·spring boot·dubbo
退休倒计时1 小时前
【每日一题】LeetCode 17. 电话号码的字母组合 TypeScript
算法·leetcode·typescript
kuonyuma2 小时前
java之动态代理
java·开发语言
衔烛之龙12 小时前
Windows x64 构建 liboqs-java教程
java·windows·python
一路向北North2 小时前
Spring Security OAuth2.0(20):完善环境配置
java·后端·spring
Java面试题总结2 小时前
IntelliJ IDEA 从卡顿到起飞,只用改这些。。。
java·ide·intellij-idea
兰令水2 小时前
hot100【acm版】【2026.7.11/12打卡-java版本】
java·开发语言·数据结构·算法·职场和发展