[Leetcode] 238. Product of Array Except Self

多仓智能订单路由与动态优化方案设计 现在有很多商家会有多个仓库,并分布在不同的城市中,当用户下单后会根据商品库存及订单信息将订单分配到对应的仓库进行发货,如就近原则,本期基于多仓库设计一套订单路由方案,包含核心逻辑、规则优先级和实施方案,适用于多仓协同的订单自动分配场景 阅读详情

Problem: https://leetcode.com/problems/product-of-array-except-self/

Solution1:
计算第 i 位的左边乘积和右边乘积,再将左右相乘

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

时间复杂度:O(n)
空间复杂度:O(n) (因为构造了left和right两个数组)

Solution2:
不再创造新数组存储右边的乘积,而是沿用左边乘积的数组

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] ans = new int[nums.length];
        ans[0] = 1;
        // 同样的先计算左边乘积
        for(int i=1; i<nums.length;i++) {
            ans[i] = ans[i-1] * nums[i-1];
        }
        // 不再创造右边数组,直接对左边乘积数组进行计算
        int R = 1;
        for(int i=nums.length-1; i>=0; i--) {
            ans[i] *= R;
            R *= nums[i];
        }
        
        return ans;
    }
}

时间复杂度:O(n)
空间复杂度:O(1) (没有创造新的数组,ans数组根据题意不作为空间复杂度考虑因素)

大疆sdkV4以及V5webrtc推流 webrtcSDK中要用它的VideoCapturer来初始化推流 ViewdoCapturer是一个接口我们继承它 然后重建创建文件CapturerAircraft.java文件。CameraUtil.java这个是我们用来创建CapturerAircraft的一个工具。除了CapturerAircraft这个文件不一样外其他都是一样的。PlayBodyBean.java这个是我们的配置文件。下面的有个代码块第一个是v4的 第二个是v5的。接着我们来创建几个推流用到的webrtc工具。 阅读详情

相关推荐

全球尺度遥感云计算平台:Google Earth Engine

Google Earth Engine(GEE)是一个基于 Google 云服务基础设施的遥感大数据分析平台,它结合 Google 强大的云计算能力与 NASA、ESA、NOAA 等机构的空间数据,用于在全球尺度解决一些非常重要的社会问题:森林退化、粮食安全、灾害预警、水资源管理、气候监测以及环境保护等。 GEE 官方宣传视频(版权归 Google 所有) Spacefan 的视频 ·303播放 发展背景 2008 年,手握 40 多年 LandSat 数据的美国地质调查局(USG...

qq_41240287的博客 5027

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

ATAC-seq数据分析流程

Outline0. ATAC-seq原理0.1 染色体结构0.2 原理0.3 结果0.4 意义1. ATAC-seq数据分析流程1.1 上游分析(1)序列质控和比对(2)序列筛选1.2 下游分析1.3 进阶分析 0. ATAC-seq原理 0.1 染色体结构 0.2 原理 0.3 结果 0.4 意义 1. ATAC-seq数据分析流程 1.1 上游分析 (1)序列质控和比对 fastqc trim_golare bowtie2 mapping时要加上参数 --very-sensitive -X 2000

weixin_57830892的博客 1万+

leetcode 238: Product of Array Except Self

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

西施豆腐渣 1万+

Wafer的T7 Code是什么,怎么来的

在半导体Cim系统中,waferstart 业务需要支持 Sorter 读取 Wafer 的 T7 Code,与 MES 的 Wafer ID 进行绑定,以便向 前追溯 Wafer 历史,那么问题来了,T7code 是什么Wafer的T7 Code是一种,用于标识双面抛光的晶圆。这种标记采用二维矩阵代码符号,适用于各种尺寸的晶圆,不仅限于450mm无缺口晶圆7。T7 Code是衬底来料背面通常会有的供应商刻的追溯ID,这是一串数字和字母组成的流水码。

阿拉伯梳子的专栏 7977

如何在Android上使用OpenCV进行摄像头标定

如何在Android上使用OpenCV进行摄像头标定,我们这里使用官方一个例程,其实并不难,只是网上的资料少了而已。相机标定是计算机视觉工作的前提,为了确定相机矩阵和畸变参数。

花草的博客 3448

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

MySQL 优化 - index_merge 导致查询偶发变慢

今天遇到了一个有意思的问题,线上数据库 CPU 出现了偶发的抖动。定位到原因是一条查询语句偶发变慢造成的,随后通过调整表中的索引解决。优化器通过某种机制检测到 index_merge 能带来性能提升,某些情况下不会带来提升,反而会耗费更长的时间,属于优化器的缺陷,可以通过调整表中的索引来解决。

分享既学习 1260

LeetCode 238. Product of Array Except Self 题解

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

cannonjinx的博客 4271

在windows上,在WSL 2(windows subsystem linux) 里一步步安装vLLM

本文介绍了在Windows系统上通过WSL2配置GPU加速的AI开发环境的完整流程。主要包括四个步骤: 1)安装WSL2并配置Ubuntu环境; 2)安装NVIDIA驱动和CUDA工具包; 3)搭建Python虚拟环境并安装vLLM; 4)验证环境并启动AI模型服务。

2601_96126911的博客 425

LVGL 9Text Area 控件 (lv_textarea) 使用指南

是一个可编辑的文本输入控件,通常用于在用户界面中输入和编辑多行或单行文本。该控件支持文本插入、删除、光标控制、文本选择、密码输入等功能。它的文本内容可以通过键盘、API 或事件进行修改,还可以设置最大文本长度、占位符文本和密码模式等。是一个强大且灵活的文本输入控件,适用于多种用户输入场景,支持文本插入、删除、光标控制、密码输入等功能。通过 LittlevGL 提供的丰富 API,开发者可以自定义文本区域的外观和行为,实现复杂的文本输入需求。

m0_62599305的博客 1683

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

相位偏折PMD全栈技术拆解 | 四步相移解算+梯度积分形貌重建,助力镜面工件微米级缺陷高精度稳定检测

工业精密检测领域中,高反光镜面类工件长期存在检测瓶颈,涵盖汽车高亮车漆、半导体抛光晶圆、光学透镜、精密抛光模具、玻璃盖板等主流产品。传统2D机器视觉仅能识别表面明显污渍、大尺寸破损,完全无法捕捉微米级划痕、凹坑、橘皮、曲面微形变等微观缺陷;常规线激光3D、普通结构光3D检测设备,面对镜面材质会产生严重镜面反射杂光,激光条纹、结构光图案完全失真,必须喷涂显影粉辅助检测,不仅破坏工件表面完整性,还无法适配自动化量产产线,检测效率与良率极低。

getusushu的博客 553

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

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

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

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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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

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 进阶的专栏 1677

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

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 题解

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

EricJeffrey的博客 214

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

LeetCode238Product of Array Except Self

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

无悔王冠的专栏 821

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

qq_38702697的博客 180
上一篇: [Leetcode] 67. Add Binary
下一篇: [Leetcode] 253. Meeting Rooms II
Shlyan
博客等级 码龄9年 0粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值