当前位置:网站首页>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-155-最小栈
- zypper命令使用示例
- String copy function
- 2021 Niuke multi school 5 double strings
- A problem of setting the private library of golang
- JVM | local method interface; Native Method Stack
- Relatively perfect singleton mode
- As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
- 相对完善的单例模式
- Redis basic data type (hash)
猜你喜欢
![[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)](/img/73/2ec957d58616d692e571a70826787f.jpg)
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)

多态的所有特征

JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)
![[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]

LabVIEW Arduino电子称重系统(项目篇—1)

Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?

继承的所有特征

行而不辍,未来可期|云扩科技入选上海市专精特新企业

Leetcode-98- validate binary search tree

Database daily question --- day 9: salesperson
随机推荐
Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
行而不辍,未来可期|云扩科技入选上海市专精特新企业
Jenkins+allure integrated report construction
Chain storage structure of linear table
How to import workflows provided on SAP API hub to sap BTP
Builder pattern
Redis basic data type (list)
LeetCode-32-最长有效括号
Some error reporting assemblies of cann code
RPA+低代码助推品牌电商启新创变、重启增长
LeetCode-155-最小栈
AC自动机
线性表的链式存储结构
JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
Expérience 10 génération de courbes bezier - amélioration expérimentale - génération de courbes B - spline par point de contrôle
剑指Offer 29.顺时针打印矩阵
LeetCode-76-最小覆盖子串
RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation
Leetcode-98- validate binary search tree