当前位置:网站首页>Leetcode 238. 除自身以外数组的乘积
Leetcode 238. 除自身以外数组的乘积
2022-07-23 18:26:00 【我不是萧海哇~~~~】

给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。
题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。
请不要使用除法,且在 O(n) 时间复杂度内完成此题。
示例 1:
输入: nums = [1,2,3,4]
输出: [24,12,8,6]
示例 2:
输入: nums = [-1,1,0,-3,3]
输出: [0,0,9,0,0]
提示:
- 2 <= nums.length <= 105
- -30 <= nums[i] <= 30
- 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内
Code:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
int left=1,right=1; //left:从左边累乘,right:从右边累乘
vector<int> res(n,1);
for(int i=0;i<n;++i) //最终每个元素其左右乘积进行相乘得出结果
{
res[i]*=left; //乘以其左边的乘积
left*=nums[i];
res[n-1-i]*=right; //乘以其右边的乘积
right*=nums[n-1-i];
}
return res;
}
};
边栏推荐
- Sui of the public chain (New Public chain project established by former Facebook /meta employees)
- DHCP:在网络中防止 Rogue DHCP Server
- socat 使用「建议收藏」
- 能量原理与变分法笔记15:微元法的求解
- Home NAS server (3) | SSD cache acceleration mechanical hard disk
- USB3.0: layout guide for vl817q7-c0
- Energy principle and variational method note 17: generalized variational principle (identification factor method)
- PowerCLi 添加esxi主机到vCenter
- 测试如何应对新的开发模式?
- Boundschecker usage "recommended collection"
猜你喜欢

PowerCLi 管理VMware vCenter 一键批量部署OVF

PowerCLi 管理VMware vCenter 一键批量部署OVA

能量原理与变分法笔记12:最小势能原理

《自适应机器人交互白皮书》
![Atcoder regular contest 144 [VP record]](/img/8c/bcffb95cb709087103bb79b8a358d1.png)
Atcoder regular contest 144 [VP record]

Powercli management VMware vCenter one click batch deployment OVA

Powercli imports licensekey to esxi

Powercli management VMware vCenter batch deployment export import

能量原理与变分法笔记19:最小余能原理+可能功原理
![Mysql database [Database Foundation -- introduction]](/img/a9/429beb3a0be7c82084e803e3165d92.png)
Mysql database [Database Foundation -- introduction]
随机推荐
Powercli moves virtual machines from host01 host to host02 host
[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计
(干货)结合Scikit-learn介绍几种常用的特征选择方法
Paddle implementation, multidimensional time series data enhancement, mixup (using beta distribution to make continuous random numbers)
White paper on adaptive robot interaction
【开发经验】开发项目踩坑集合【持续更新】
What is weak network testing? Why should weak network test be carried out? How to conduct weak network test? "Suggested collection"
R语言筛选dataframe指定的数据列、R语言排除(删除)dataframe中的指定数据列(变量)
Attack and defense world web question Fakebook
C language leak detection and filling (1)
GPS北斗时钟服务器(NTP网络时钟系统)施工部署方案
Cannot read properties of null (reading ‘pickAlgorithm‘)
MySQL 数据恢复 —— 使用 data 目录
进程调度的基本过程
树莓派3b串口登录前准备工作
Canvas draw text and clear draw
编译器LLVM-MLIR-Intrinics-llvm backend-instruction
Ggarrange function of R language ggpubr package combines and annotates multiple images_ Figure add annotation, annotation, annotation information to the combined image, and add annotation information
Decryption: PTP clock synchronization in intelligent substation (Beidou clock server)
基于自学习的机器人决策系统(达闼科技赵开勇)