【陪伴式刷题】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)
相关推荐
coderSong256836 分钟前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
Mr_Air_Boy1 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
豆沙沙包?2 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
年老体衰按不动键盘2 小时前
快速部署和启动Vue3项目
java·javascript·vue
咖啡啡不加糖2 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
liuyang-neu3 小时前
java内存模型JMM
java·开发语言
UFIT3 小时前
NoSQL之redis哨兵
java·前端·算法
刘 大 望3 小时前
数据库-联合查询(内连接外连接),子查询,合并查询
java·数据库·sql·mysql
怀旧,3 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
大春儿的试验田4 小时前
Parameter ‘XXX‘ not found. Available parameters are [list, param1]
java