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

[思路]:刚看到题时我想到的是用两重循环直接求解,这样实现很简单。

java代码:

public class Solution {
     public static int[] productExceptSelf(int[] nums) {
         int [] product=new int[nums.length];    
         for(int i=0;i<nums.length;i++){
             product[i]=1;
             for(int j=0;j<nums.length;j++){
                 if(j!=i)
                 product[i]*=nums[j];
             }
         }
            return product;
        }
}

测试结果正确,提交上去毫无意外地超时了。

继续思考,对于一个给定的数组nums[1,2,3,4],我们用一个res[4]的数组来存放结果,res中存放的数据应是

res[0]=2*3*4

res[1]=1*3*4

res[2]=1*2*4

res[3]=1*2*3

这样我们可以发现一些规律:

1.数组的两端直接就是正确的结果

2.两个数组的中间部分相乘就是正确结果

java代码:

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

 

转载于:https://www.cnblogs.com/rever/p/4889381.html

FineReport实战-- 国内外各地区销售数据分析 多个图表联动 进行国内外各地区业务员销售数据分析是一项复杂但极具价值的任务,它可以帮助企业了解不同市场的表现,并据此调整销售策略以提高整体绩效。实现功能:同一个报表里面多个图表联动问题。 阅读详情

相关推荐

从摄像头选型到PCB布线:搞懂MIPI CSI带宽计算,避免你的嵌入式项目翻车

本文详细解析了MIPI CSI带宽计算在嵌入式项目中的关键作用,从摄像头选型到PCB布线提供全流程指导。通过实际案例和计算公式,帮助工程师准确计算带宽需求,避免图像丢帧和系统崩溃问题,同时优化PCB设计确保信号完整性。

weixin_30533109的博客 383

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

echarts 生成中国陕西省份(或其他省份)的地图,大屏使用

效果图: 一、首先第一步需要下载echarts和获取到陕西身份的地图信息。 1.下载echarts链接(官网): Apache ECharts 2.生成指定省份的地图数据信息,数据可以在阿里云获取。 DataV.GeoAtlas地理小工具系列 如图,点击左圈需要的省份,这里我选择的是陕西省,然后点击右圈复制下载链接,得到一个json格式的数据,可以在json转换网站转成js格式。 Json在线解析格式化-Json在线校验Json格式化压缩-Jsons在线工具 转成js格.

PrimaryColor 3896

leetcode 238: Product of Array Except Self

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

西施豆腐渣 1万+

Ansys HFSS 中的同轴电缆仿真

准确模拟它们的行为对于确保 RF 系统的最佳性能至关重要。在本博客中,我们将介绍在 HFSS 中设置同轴电缆仿真并绘制插入损耗、回波损耗和电场分布的过程。将波端口分配给同轴线的两端以激励模型,并定义了从 1 GHz 到 20 GHz 的频率扫描以评估其宽带性能。铜分配给内部导体,铝分配给外部导体。介电区域使用 PTFE 定义,其相对介电常数为 2.1,损耗角正切为 0.00022。仿真完成后,结果用于绘制整个频率范围内的回波损耗和插入损耗。此外,电介质内的电场分布也被绘制和动画化。

jkh920184196的博客 1926

MPC模型预测控制实战:从理论到代码实现(Python示例)

本文详细介绍了MPC模型预测控制的实战应用,从理论到Python代码实现。通过构建系统模型、预测方程、优化问题及约束处理,展示了如何实现一个完整的MPC控制器。文章还提供了水箱液位控制的应用案例,帮助读者掌握MPC在工业控制中的实际应用。

weixin_42653691的博客 467

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

香橙派5玩转SATA SSD:一个被忽略的overlays配置项,让你的Ubuntu系统飞起来

本文详细介绍了如何在香橙派5上通过配置`overlays=ssd-sata`启用SATA SSD作为系统启动盘,大幅提升Ubuntu系统的性能。文章深入解析了设备树覆盖的工作原理,提供了完整的配置流程和性能优化建议,帮助开发者充分发挥香橙派5的硬件潜力。

weixin_30787531的博客 228

LeetCode 238. Product of Array Except Self 题解

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

cannonjinx的博客 4271

发票OFD格式转换成PDF

发票OFD格式转换成PDF

Be_insighted的博客 942

高云FPGA系列教程(2):FPGA点灯工程创建、程序下载和固化

相比于国外FPGA厂商EDA开发环境,几个G到几十个G的大小,高云云源软件安装包也很小巧,界面也很简洁,综合速度非常快,是我没想到的,好像在使用单片机一样,点一下就编译完成了,以本文简单的LED示例工程为例,比特流文件几乎在几秒钟内生成;无论是下载比特流文件到RAM还是Flash中,速度都很快,而且只有一种fs程序文件格式,不用再进行bit/mcs转换;云源软件自带的编辑器也很好用,和Notepad++界面差不多,而且快捷键很方便,具有基本的自动补全功能。

whik1194的博客 5466

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的博客 176

VCS NLP 低功耗验证系列(六):功耗分析与优化——SAIF 报告与验证效率提升

本文是VCS NLP低功耗验证系列的第六篇,主要解决功耗数据采集和验证效率优化问题。文章首先介绍了SAIF(Switching Activity Interchange Format)文件在功耗分析中的作用,详细讲解了VCS的Region-Based SAIF功能,包括三个关键系统任务($saif_region_create、$saif_region_exclude、$saif_region_monitor)的使用方法和工程实践建议。接着从编译侧、运行侧和方法学侧三个维度提出了NLP验证效率优化策略,特别强

元直的博客 478

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的博客 318

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Camera2Demo

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

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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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 229

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

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的博客 621

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

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

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七杂⑧ 175

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

EricJeffrey的博客 214

LeetCode: 238. Product of Array Except Self

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

杨领well的专栏 185

LeetCode238Product of Array Except Self

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

无悔王冠的专栏 821
上一篇: LeetCode (262):Nim Game
下一篇: LeetCode(144):Binary Tree Preorder Traversal
dixie2009
博客等级 码龄11年 3粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值