当前位置:网站首页>238.除自身以外数组的乘积
238.除自身以外数组的乘积
2022-06-11 21:36:00 【爱学代码的学生】
题目描述:
给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。
题目分析:
此题我们使用前后缀法
对于此题我们可以理解我们需要将下标为i之前下标为0~i-1的元素和下标为i+1~n的元素之积。
那对于此题我们可以设置一个数组用来存储此元素之前的元素之积(前缀)。
然后从最后一个元素进行乘积(尾缀)。
代码实现如下:
int* productExceptSelf(int* nums, int numsSize, int* returnSize){
int* a = (int*)malloc(numsSize*sizeof(int));//设置一个新数组
int add = 1;
for (int i = 0; i < numsSize; i++)
{
a[i] = add; //第一个元素前没有元素所以存储1(乘积后也为本身)
add *= nums[i];
}
add = 1;
for (int i = numsSize - 1; i >= 0; i--)
{
a[i] *= add; //从后向前乘积,最后一个元素里存储为前面所有元素之积,所以乘1
add *= nums[i];
}
*returnSize = numsSize;
return a;
}边栏推荐
- Leetcode-76- minimum covering substring
- 领先企业推进智慧财务的同款效率工具,赶快了解一下?
- A problem of setting the private library of golang
- [Part 13] source code analysis and application details of completabilefuture class [key]
- The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
- LeetCode-32-最长有效括号
- 实验10 Bezier曲线生成-实验提高-交互式生成B样条曲线
- Leetcode-155-minimum stack
- RPA丨首席财务官如何找到数字化转型“超级入口”?
- AC自动机
猜你喜欢

类和对象(4)

RPA超自动化 | 农耕记携手云扩加速财务智能化运营

类与对象(3)

apache 本地多端口配置

Answer fans' questions | count the number and frequency of letters in the text
![[Part 14] source code analysis and application details of completionservice class [key]](/img/41/9f5383d6eafb32723e29c15da3a1af.jpg)
[Part 14] source code analysis and application details of completionservice class [key]

Codeworks round 744 (Div. 3) problem solving Report

Database daily question --- day 9: salesperson

效率起飞啊!还能这样开发的?

LabVIEW控制Arduino实现红外测距(进阶篇—6)
随机推荐
flutter系列之:flutter中常用的container layout详解
[Part 13] source code analysis and application details of completabilefuture class [key]
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称试读版
RPA超自动化 | 农耕记携手云扩加速财务智能化运营
Educational Codeforces Round 111 (Rated for Div. 2) C 补题
LeetCode-110-平衡二叉树
Leetcode-322- change exchange
How to import workflows provided on SAP API hub to sap BTP
JVM|前言介绍
Codeforces Round #744 (Div. 3) 解题报告
Common file functions
Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
剑指Offer 29.顺时针打印矩阵
LabVIEW Arduino电子称重系统(项目篇—1)
Game client performance (memory) [previous]
ESP32C3 Arduino库使用方法
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称
Redis basic data type (set)
线性表的链式存储结构
The same efficiency tool for leading enterprises to promote smart finance. Let's have a quick look?