238. 除了自身以外数组的乘积 - 力扣(LeetCode)

方法 1:暴力法

  • 时间复杂度:O(n²) ❌ 不符合题目要求
  • 空间复杂度:O(1)(不计输出数组)
python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 16:25 
# "Stay hungry,stay foolish."


class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """

        n = len(nums)
        answer = []
        for i in range(n):
            prod = 1
            for j in range(n):
                if j != i:
                    prod *= nums[j]
            answer.append(prod)
        return answer

方法 2:左右乘积列表

  • 时间复杂度:O(n) ✅
  • 空间复杂度:O(n)(用了两个额外数组)
python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 16:25 
# "Stay hungry,stay foolish."


class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """

        n = len(nums)
        left = [1] * n
        right = [1] * n
        for i in range(1, n):
            left[i] = left[i -1] * nums[i - 1]

        for i in range(n - 2, -1, -1):
            right[i] = right[i + 1] * nums[i + 1]

        res = []
        for i in range(n):
            res.append(left[i] * right[i])

        return res

逐步分析:

1. 初始化
复制代码
n = len(nums)
left = [1] * n    # [1, 1, 1, 1]
right = [1] * n   # [1, 1, 1, 1]
2. 计算 left 数组(从左到右)
复制代码
# i=1: left[1] = left[0] * nums[0] = 1 * 1 = 1
# i=2: left[2] = left[1] * nums[1] = 1 * 2 = 2
# i=3: left[3] = left[2] * nums[2] = 2 * 3 = 6
# left = [1, 1, 2, 6]
3. 计算 right 数组(从右到左)
复制代码
# i=2: right[2] = right[3] * nums[3] = 1 * 4 = 4
# i=1: right[1] = right[2] * nums[2] = 4 * 3 = 12
# i=0: right[0] = right[1] * nums[1] = 12 * 2 = 24
# right = [24, 12, 4, 1]
4. 合并结果
复制代码
result = []
result[0] = left[0] * right[0] = 1 * 24 = 24
result[1] = left[1] * right[1] = 1 * 12 = 12
result[2] = left[2] * right[2] = 2 * 4 = 8
result[3] = left[3] * right[3] = 6 * 1 = 6
# result = [24, 12, 8, 6]

方法 3:空间优化版

  • 时间复杂度:O(n) ✅
  • 空间复杂度:O(1) ✅
python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 16:25 
# "Stay hungry,stay foolish."


class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """

        n = len(nums)
        answer = [1] * n
        for i in range(1, n):
            answer[i] = answer[i - 1] * nums[i - 1]


        R = 1
        for i in range(n - 1, -1, -1):
            answer[i] *= R
            R *= nums[i]

        return answer

结果

解题步骤:https://www.bilibili.com/video/BV1GbrCBmEqN/?vd_source=15b4bc8968fa5203cc470cb68ff72c96

相关推荐
小O的算法实验室1 天前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
郭涤生1 天前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能1 天前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****1 天前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能1 天前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能1 天前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo1 天前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
Proxy_ZZ01 天前
从零实现LDPC比特翻转译码器:C语言实战与底层逻辑解析
c语言·算法