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).

For example, given [1,2,3,4], return [24,12,8,6].

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

分析:题目要求不能用除法并且时间复杂度要为O(n),这是本题的难点,本题最直观的解法是用循环遍历,代码如下:

public class Solution {
    public int[] productExceptSelf(int[] nums) {
        int [] product = new int[nums.length];
        for(int m = 0; m < nums.length; m++){
            product[m] = 1;
        }
        for(int i = 0; i < nums.length; i++){
            for(int j = 0; j < nums.length ; j++){
                if(j == i) {
                    continue;
                }
                product[i] *= nums[j];
            }
            
        }
        return product;
    }
}
但是此算法的时间复杂度超过要求,可以选择用两个数组(left[]和right[])来分别接收nums[]数组中从左到右和从右到左相乘,值得一提的是,left[]的首元素代表nums[]第零个元素左边的元素之积,不存在可以赋值为1,同理将right[]数组的最后一位也赋值为1。最终结果数组的第i个元素由左右数组的第i个数据相乘即可得到。

代码:

public class Solution {
    public int[] productExceptSelf(int[] nums) {
        int [] product = new int[nums.length];
        int [] left = new int[nums.length];
        int [] right = new int[nums.length];
        left[0] = 1;
        right[nums.length - 1] = 1;
        for(int i = 0;i < nums.length - 1;i++){
            left[i + 1] = left[i] * nums[i];
        }
        for(int i = nums.length - 1;i > 0; i--){
            right[i - 1] = right[i] * nums[i];
        }
        for(int i = 0; i < nums.length; i++){
            product[i] = left[i] * right[i];
        }
        return product;
    }
}

Follow up中提到能否用常量空间复杂度,可以将结果数组存放左边/右边相乘后的结果,然后再和右边/左边相乘后的结果相乘即可得到结果。

代码:

public class Solution {
    public int[] productExceptSelf(int[] nums) {
        // int [] product = new int[nums.length];
        // int [] left = new int[nums.length];
        // int [] right = new int[nums.length];
        // left[0] = 1;
        // right[nums.length - 1] = 1;
        // for(int i = 0;i < nums.length - 1;i++){
        //     left[i + 1] = left[i] * nums[i];
        // }
        // for(int i = nums.length - 1;i > 0; i--){
        //     right[i - 1] = right[i] * nums[i];
        // }
        // for(int i = 0; i < nums.length; i++){
        //     product[i] = left[i] * right[i];
        // }
        // return product;
        int [] product = new int[nums.length];
        product[0] = 1;
        for(int i = 0; i < nums.length - 1; i++){
            product[i + 1] = product[i] *nums[i];
        }
        int right = 1;
        for(int i = nums.length - 1; i >= 0; i--){
            product[i] *= right;
            right *= nums[i];
        }
        return product;
    }
}




coze 怎么读取excel文件,使用循环,数据库完成LLM问答并存储 本文介绍了如何在Coze平台实现"Excel读取+循环处理+LLM问答+数据库存储"的工作流。主要分为三个步骤:1)使用代码节点解析Excel文件为问答对数组;2)通过循环节点遍历问答对,调用LLM节点生成回答;3)利用数据库节点将问答结果存储到Coze内置数据库中。文中提供了代码示例和节点配置说明,并强调了注意事项如Excel格式要求、LLM调用频率控制等。该工作流适用于批量构建知识库问答系统并持久化存储结果。 阅读详情

相关推荐

Cadence PCB SI仿真实战:如何手动添加VIA过孔模型提升板级链路精度

本文详细介绍了在Cadence PCB SI仿真中如何手动添加VIA过孔模型以提升DDR4/5设计的精度。通过参数化建模和三维全波建模技术,工程师可以有效解决高速设计中的信号完整性问题,显著降低仿真误差。文章还提供了实战案例和常见问题解决方案,帮助优化板级链路性能。

weixin_28683391的博客 408

leetcode-238. Product of Array Except Self

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 ofnumsexceptn...

anchouman0644的博客 159

Arduino创意作品实战案例:手把手教你做温控风扇

通过这个实用的arduino创意作品,轻松实现温度感应与风扇自动控制,结合传感器与编程逻辑,打造智能散热系统,是入门者理想的arduino创意作品实践项目。

weixin_42311427的博客 608

leetcode 238: Product of Array Except Self

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

西施豆腐渣 1万+

SystemVue 链路预算、仿真分析步骤

使用SystemVue进行链路预算、频谱分析等,相对ADS更全面。

BeCourageable的博客 3836

Qt5.15.2安卓开发环境配置保姆级教程(含JDK8、SDK33、NDK21.4及Gradle国内镜像避坑指南)

本文提供Qt5.15.2安卓开发环境配置的完整教程,涵盖JDK8、SDK33、NDK21.4及Gradle国内镜像设置,帮助开发者快速搭建开发环境并避免常见问题。通过详细步骤和实战技巧,解决依赖冲突、网络超时等难题,提升开发效率。

weixin_28730353的博客 203

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

基于Ansys Fluent 的颗粒分离/过滤解决方案

过滤是指通过特殊装置将颗粒移除,将流体提纯净化的过程。过滤的方式很多,应用的物系也很广泛,固-液、固-气、大颗粒-小颗粒等。

sh_djoin的博客 1852

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 except ...

qq_43271202的博客 175

微信小程序获取用户手机号全流程实战(附完整代码)

本文详细介绍了微信小程序获取用户手机号的全流程实战,包括前端授权、后端解密及安全合规要点。通过传统授权按钮和新版手机号快速验证组件两种方案,帮助开发者高效实现功能,并附完整代码示例。重点解析了微信小程序接口的使用技巧和常见问题解决方案,确保数据安全与用户体验。

weixin_29280219的博客 568

ZYNQ新手必看:5分钟搞定SD卡与QSPI Flash双启动模式配置(附常见问题排查)

本文为ZYNQ开发者提供了SD卡与QSPI Flash双启动模式的完整配置指南与深度排错方法。详细解析了从BootROM、FSBL到BOOT.bin的启动链条,并分步讲解了在Vivado/Vitis中的硬件配置、软件工程创建及BOOT.bin文件生成流程。针对SD卡格式化、文件放置以及QSPI Flash烧录中的常见陷阱(如版本差异、时钟配置)给出了具体解决方案和排查清单,帮助开发者快速实现可靠的产品启动与程序固化。

ll5678的博客 932

LeetCode 238——Product of Array Except Self

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

CrazyOnes的博客 316

基于YOLOv8与UI界面的校园垃圾分类督导系统

YOLO(You Only Look Once)是目前最为广泛使用的目标检测算法之一。它通过一次前向传递即完成目标检测,具有极高的速度与效率。YOLOv8是YOLO系列的最新版本,相比于之前的YOLO版本,YOLOv8在速度、精度、检测能力等方面都有了显著的提升。本项目通过结合YOLOv8模型与UI界面,成功实现了校园垃圾分类督导系统。该系统能够自动识别不同类型的垃圾,并通过UI界面实时展示垃圾分类的结果。

YOLO 337

LeetCode Top 100 Liked Questions 238. Product of Array Except Self (Java版; Medium)

welcome to my blog LeetCode Top 100 Liked Questions 238. Product of Array Except Self (Java版; Medium) 题目描述 Given an array nums of n integers where n > 1, return an array output such that output[i] ...

littlehaes的博客 142

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode 238. Product of Array Except Self 题解

本题是前缀积技巧的经典应用。分别计算前缀积和后缀积空间优化:直接在输出数组中计算不使用除法。

cannonjinx的博客 4021

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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 except nums...

Wisimer 225

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode 238. Product of Array Except Self (python)

Leetcode 238. Product of Array Except Self题目解法1:构建左右product数组解法2:动态构建product数组 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product ...

qq_37821701的博客 619

[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).

coder 进阶的专栏 1676

Leetcode238. Product of Array Except Self

给定数组nums,返回输出数组output,其中output[i]是nums数组中除了nums[i]元素外的所有元素乘积

magic_jiayu的博客 240

238. Product of Array Except Self leetcode java

题目:Given an array of n integers where n &gt; 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...

Superlin的博客 330

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

Leetcode 238. Product of Array Except Self

题目描述:给出一个数组,返回一个数组,ans[i]是nums[i]以外的其他乘积。 但是这道题禁止我们使用除法. 题目链接:Leetcode 238. Product of Array Except Self 思路:先计算一个总的乘积,如果为0,代表数组中有0元素,则直接找到那个索引,然后计算其他元素的乘积,对应其他的肯定是0. 要求O(1)空间、O(n)时间,数组的题目一般就是遍历一次,...

Za七杂⑧ 174

LeetCode 238 Product of Array Except Self 题解

LeetCode 238 Product of Array Except Self 题解 题意 给一个数组nums,输出一个新数组output,其中output[i]是原数组中除nums[i]以外,其他所有元素的乘积。要求在O(n)的时间内解决并且不使用除法,而且不使用除了返回结果以外的额外空间。 思路 前缀积 * 后缀积。首先可以判断出的是,结果不会超过int的最大值,否则返回值也不应该是vec...

EricJeffrey的博客 214

LeetCode238Product of Array Except Self

先上题目:题目大意:给一数组。返回一数组,这个数组对应原数组除了对应数其他数的乘积和。注意为额外的O(n)空间技巧:题目中已经明确把最容易想到的求所有数乘积,然后分别除每一个数的简单做法给否定了,因为这个也太没技术含量了。。。这里要提及一个经常用到的概念,前缀和后缀,这个概念对很多程序有至关重要的意义以后如果要介绍KMP算法,其中就用到了字符串前缀和后缀的匹配。其实前缀和后缀顾名思义我们可以把数组中

无悔王冠的专栏 821

Leetcode_238_Product of Array Except Self

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48598939 Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the pr

皮斯特劳沃 1483
上一篇: LeetCode 136.137. 260.Single Number ⅠII III
下一篇: LeetCode 242. Valid Anagram
Appletable
博客等级 码龄14年 0粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值