【陪伴式刷题】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)
相关推荐
shehuiyuelaiyuehao1 小时前
1计算机是如何工作的
java·计算机网络
letisgo52 小时前
JAVA 高级进阶07篇《@Transactional失效的8个场景:代理机制到传播行为》
java·面试·transactional·spring事务·aop动态代理
水域安全老周2 小时前
水趣钓鱼救生衣专利拆解:两级锁紧如何解决落水人衣分离
java·前端·网络
三8442 小时前
Java 模板注入(FreeMarker) · 02 · 模板引擎与 FreeMarker 语法
java·开发语言·web安全·freemarker
暮雨哀尘2 小时前
Java 基础练习(一):创建类并调用对象
java·开发语言
想要入门的程序猿3 小时前
回调函数学习
java·网络·学习
泡海椒3 小时前
动态代理核心原理:JQuick-Java接口规则自动生成机制解析
java·开发语言·python
步行cgn3 小时前
Spring 启动报错:BeanFactory not initialized 的原因与解决
java·python·spring
步行cgn3 小时前
Spring 报错:No bean class specified on bean definition 的原因与解决
java·后端·spring
zhangjw344 小时前
第47篇:Java综合实战:电商秒杀系统(高并发场景)
java·开发语言