LeetCode:238. Product of Array Except Self

如何使用AKShare作为OpenBB的数据源 `openbb_akshare`项目是 OpenBB 的数据源扩展。可以用于查询A股和港股数据 阅读详情

LeetCode:238. Product of Array Except Self

Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]
Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

给定一个长度为n的整型数组nums,对于某个位置i,输出数组output在该位置上的元素是nums中除了i位置元素之外所有元素的乘积。求最终的output数组。

思路:乘积 = 当前数左边的乘积 * 当前数右边的乘积

以数组 [2,3,4,5]为例,以 left 数组来记录某个位置左边的所有元素乘积,以right 数组来记录某个位置右边的所有元素乘积,那么对于最终的输出结果output,里面的某个位置的元素值为output[i] = left[i] * right[i]。

Python 代码实现

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        l = len(nums)
        res = [0]*l
        left = [1]*l
        right = [1]*l
        leftProduct = 1
        rightProduct = 1
        for i in range(1,l):
            leftProduct *= nums[i-1]
            left[i] = leftProduct
            
        for i in range(0,l-1)[::-1]:
            rightProduct *= nums[i+1]
            right[i] = rightProduct
            res[i] = left[i]*right[i]
        res[l-1]=left[l-1]
        return res 

由于对空间复杂度还有要求,所以改进一下,取出left和right两个数组,直接用res数组暂存:

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        l = len(nums)
        res = [1]*l
        
        leftProduct = 1
        rightProduct = 1
        for i in range(1,l):
            leftProduct *= nums[i-1]
            res[i] = leftProduct                
        for i in range(0,l-1)[::-1]:
            rightProduct *= nums[i+1]
            res[i] = res[i]*rightProduct
        return res

THE END.

Flink实战(101):双流join(三)双流中实现Inner Join、Left Join与Right Join 声明:本系列博客是根据SGG的视频整理而成,非常适合大家入门学习。 《2021年最新版大数据面试题全面开启更新》 简介 之前文章中提到JoinedStream与CoGroupedStream,例如下列代码: dataStream.join(otherStream) .where(0).equalTo(1) .window(TumblingEventTimeWindows.of(Time.seconds(3))) .apply { ... } 由于joinedStream与 阅读详情

相关推荐

Rocky Linux 9 远程桌面配置全攻略:从零搭建XRDP服务(含防火墙设置)

本文提供了在Rocky Linux 9上从零开始配置XRDP远程桌面服务的完整指南。详细介绍了系统准备、XRDP服务安装、防火墙与SELinux安全设置、多用户性能优化以及多平台客户端连接实测。重点解决了使用Windows远程桌面协议(RDP)连接Rocky Linux时常见的配置难点与安全问题,帮助用户获得稳定高效的图形化远程访问体验。

vodka的博客 661

leetcode 238. Product of Array Except Self-数组乘积

原题链接:238. Product of Array Except Self 【思路-Java、Python】 根据Note: The output array does not count as extra space for the purpose of space complexity analysis. 我们还是可以申请大小为 n 的空间 result 用于返回结果,由于题目要求

hdtopku 1038

CarPlay无线连接开发实战:从蓝牙EIR查询到iAP2认证的完整代码逻辑梳理

本文详细解析了无线CarPlay连接开发的完整流程,从蓝牙EIR查询到iAP2认证的代码实现。重点介绍了蓝牙握手、WiFi Beacon帧配置、热点信息准备等关键环节的实战技巧,帮助开发者快速搭建稳定可靠的无线CarPlay连接系统。

weixin_31534091的博客 332

LeetCode 高级 - Product of Array Except Self

Product of Array Except Self 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。 示例: 输入: [1,2,3,4] 输出: [24,12,8,6] 说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。 进阶: 你可以在...

whdAlive 's blog 588

CANoe数据回放实战:如何用Replay Block精准复现实车故障(附ECU数据处理技巧)

本文深入探讨了利用CANoe的Replay Block功能进行数据回放,以精准复现实车偶发性故障的实战方法。文章重点解析了从原始OBD日志中移除目标ECU数据、转换报文方向等核心数据处理技巧,旨在帮助工程师重构真实的网络测试环境,从而高效定位并解决复杂的ECU问题。

nnn11的博客 503

Chroma向量数据库实战:用Python快速搭建本地知识库(附中文诗词检索案例)

本文详细介绍了如何使用Chroma向量数据库和Python快速搭建本地知识库,并附有中文诗词检索的实战案例。通过解析核心概念、数据准备、向量化及智能检索实现,展示了Chroma在处理中文语义搜索时的轻量级与易用性,为开发者构建AI应用原型提供了实用指南。

weixin_29221753的博客 484

leetcode 238. Product of Array Except Self

题目: 题目分析: 本题输入一个列表,[1,2,3,4],输出为【24,12,8,6】,即每一个输入元素,对应输出时为,除此元素以外的其他元素的乘积。 代码分析: 1.输入为列表,输出为列表 2.考虑到输出元素为除当前元素以外的元素的乘积,故需要考虑前后 3 .方式1 :利用for 循环,i之前进行乘积,在对i之后进行乘积,最后,将两值相乘得出最后结果。  程序代码: #...

博客 小站 507

基于AXI PCIE IP的FPGA PCIE卡示意图

如果在上图中的AXI互联IP上再连接一个带AXI Slave接口的BRAM控制器,这个BRAM控制器连接一个BRAM,Host PC在系统主存中分配了一些不连续的内存,Host PC可以将这些内存段的起始地址(当然要先转为PCIE总线地址)写到AXI总线上的BRAM中,这样AXI CDMA读取BRAM,将读取到的PCIE总线基地址写到AXIPCIE寄存器块中的AXIBAR2PCIEBAR_n寄存器中,这样就实现了SG DMA。通过动态改变AXIBAR2PCIEBAR_n寄存器的值,可以实现多通道DMA。

csdn_gddf102384398的博客 1626

leetcode 【238 Product of Array Except Self】【Python】

本题给定一个数组,要求我们返回一个同等大小的数组,其中每个output[i]的值是除了nums中i位置的其他数字的乘积。第一想法是除法,但是不可以用啦 第二想法是两次循环,当然时间复杂度也是太高 继续思索可以发现每个位置其实都是它前面所有数的乘积再乘上它后面所有数的乘积。按照这种想法建立返回数组,首先该数组中从第二个数开始每个数都是前面数的乘积,这样完成了第一步,其实此时数组中的最后一个数已经是

u014251967的博客 1336

ZYNQ-Linux开发之(二)Vivado工程搭建、Block Design设计搭建、PS、PL的IP核的使用配置

其他可能需要配置的是PL Fabric Clocks,根据之后添加PL的IP核所需的时钟确定,比如本工程的PL的IP核中有的需要100M时钟,有的需要200M时钟,有的需要125M时钟,还有的需要10M时钟,则可按下图设置,对于精度要求不高的IP核,还可以通过添加时钟的IP来进行时钟的倍频实现需要的时钟。4. SD有两个可选,一般设计中SD0为SD卡槽,SD1为EMMC,若为板卡存在EMMC芯片则SD0和SD1都需要勾选,无论是否有EMMC,SD0都是必须选择的。

qq_38584212的博客 9705

游戏大厂射击游戏无缝大世界游戏DS服务器架构设计方案详解

摘要: 本文深入解析游戏大厂无缝大世界射击游戏的分布式服务器(DS)架构设计方案。以《和平精英》《量子特攻》为例,阐述如何通过动态分区、兴趣管理、弹性扩缩等核心技术应对高并发、低延迟、无缝切换等挑战。架构分为世界协调层、分区计算层、物理判定层等模块,结合微服务化、快照同步和Kubernetes调度,实现玩家跨区无感知迁移与一致判定。重点优化动态负载均衡、状态同步算法及容灾备份,依托分布式数据库与多云部署保障高可用性,为同类游戏提供可扩展的技术范本。(149字) 关键词: 无缝大世界、分布式服务器、动态分区、

本博客聚焦游戏开发技巧、创业实战心得及大学热门课程干货。这里既有技术深度,也有思维广度,无论你是开发者、创业者还是高校学子,都能在这里找到适合自己的成长之路! 568

[LeetCode]Product of Array Except Self,解题报告

题目Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For

小一的专栏 1万+

第10章《VTK自定义部件Widget》

是用于交互式操作 3D 场景的组件,例如移动对象、调整尺寸、创建标注等。VTK 提供了一些内置的 Widget,如。,用户可以点击鼠标,在 3D 场景中放置一个点。负责绘制一个点,并控制其颜色、大小等。,可用于 3D 标注、对象拾取等应用!等,但在某些应用中,我们需要。这样,我们就创建了一个。

yumeiguo的博客 525

leetcode 238: Product of Array Except Self

leetcode 238: Product of Array Except Self python, c++ java

西施豆腐渣 1万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

LeetCode 238: Product of Array Except Self

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n).

哈哈的个人专栏 4263

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode 238: Product of Array Except Self

LeetCode 238: Product of Array Except Self 题目描述 给定一个数组 A[0, 1,..., n-1],请构建一个数组 B[0, 1,..., n-1],其中 B 中的元素 B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。要求不能使用除法。 解题思路 在O(N)时间复杂度下,分别进行自左向右、自右向左两次累乘即...

HakunaMatata的博客 185

LeetCode238:Product of Array Except Self

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements ofnums except nums[i]. Solve it without division and in O(

1319

LeetCode: 238. Product of Array Except Self

LeetCode: 238. Product of Array Except Self 题目描述 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums excep...

杨领well的专栏 185

LeetCode238:Product of Array Except Self

Given an arraynumsofnintegers wheren> 1, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i]. Example: Input: [1,2,3,4] Output: ...

卷卷萌的博客 219

LeetCode 238:Product of Array Except Self

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O

judadeshu的博客 293

LeetCode:238product of Array Except Self

problem Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. solution 初始想法,设立一个等长的数组用来存储返...

erzhushashade的博客 133

leetcode:238. Product of Array Except Self(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except SelfGiven an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of

z_zhaojun的博客 1953

Leetcode: 238. Product of Array Except Self

Question Given an array numsof n integers where n > 1, return an array output such that output[i]is equal to the product of all the elements of numsexcept nums[i]. Example: Input: [1,2,3,4] Outp...

兔子窝 140

LeetCode笔记:238. Product of Array Except Self

计算除了该元素以外所有元素的乘积

Minecraft 995

LeetCode解题分享:238. Product of Array Except Self

Problem Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Ou...

DaVinciL的博客 267

Leetcode 238:Product of Array Except Self

Leetcode—238Product of Array Except SelfGiven an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Sol

Code_J_xer的博客 418

LeetCode (238):Product of Array Except Self

Given an array ofnintegers wheren> 1,nums, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i]. Solve itwithout divisionand in...

dixie2009的博客 117
上一篇: LeetCode:94. Binary Tree Inorder Traversal
下一篇: LeetCode:347. Top K Frequent Elements
XP-Code
博客等级 码龄12年 113粉丝 194原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值