当前位置:网站首页>LeetCode刷题——NO.238——除自身以外数组的乘积
LeetCode刷题——NO.238——除自身以外数组的乘积
2022-07-26 23:20: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]
进阶:你可以在 O(1) 的额外空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)
二、思路分析
左右乘积列表
1)创建两个大小跟nums相同的数组 l 和 r ,l[i] 代表i左侧所有数的乘积,r[i] 代表 i右侧所有数的乘积。
2)填充两个数组,对l来说有:l[i] = l[i - 1] * nums[i - 1];这里要注意得先把最左侧的数置为1:l[0] = 1; 对r来说有:r[i] = r[i + 1] * nums[i + 1];这里要注意得先把最右侧的数置为1:r[numsSize - 1] = 1;
3)最后每个位置的值为:r[i] * l[i];
如图:
右侧也同理
三、代码
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
int* l = (int*)malloc(sizeof(int) * numsSize);
int* r = (int*)malloc(sizeof(int) * numsSize);
l[0] = 1;
r[numsSize - 1] = 1;
for (int i = 1; i < numsSize; i++)
{
l[i] = l[i - 1] * nums[i - 1];
}
for (int i = numsSize - 2; i >= 0; i--)
{
r[i] = r[i + 1] * nums[i + 1];
}
for (int i = 0; i < numsSize; i++)
{
r[i] *= l[i];
}
*returnSize = numsSize;
return r;
}
四、进阶
上面的方法虽然解决了问题,但是空间复杂度不为O(1)。为O(N)(nums的大小)
这里说明一点:输出数组不算在空间复杂度内。
所以我们只需要创建l数组,从右往左时直接创建个变量R代替,R累乘nums的数值:
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* productExceptSelf(int* nums, int numsSize, int* returnSize){
int* l = (int*)malloc(sizeof(int) * numsSize);
l[0] = 1;
for(int i = 1; i < numsSize; i++)
{
l[i] = l[i - 1] * nums[i - 1];
}
int R = 1;
for(int i = numsSize - 2; i >= 0; i--)
{
R *= nums[i + 1];
l[i] *= R;
}
*returnSize = numsSize;
return l;
}
边栏推荐
- Fist guessing applet based on Object-C novice on the road
- 进程的调度
- 最新多线程&高并发学习资料,面试心里有底气
- OSPF basic experimental configuration
- 软件测试基础理论知识—概念篇
- HCIP-第一天
- Summary of dataset operations in ppocrlabel format.
- [C language] relevant distinction between strlen and sizeof
- It has been established for 3 years, and now goose factory has an annual income of millions +. As some suggestions of software testing predecessors
- C language student information management system can access text files based on arrays
猜你喜欢
随机推荐
OSPF basic experimental configuration
swiperjs自定义宽度
uni-app 微信小程序搜索关键字标红显示
[dimension reduction blow, take you to learn CPU in depth (Part 1)]
Go language slow start - package
NAT network address translation protocol topology experiment
Static routing experiment configuration
Hcip day 3 Wan topology experiment
通过ensp让静态路由实现全网可达
JUC concurrent programming
hcip--ospf接口网络接口类型实验
MGRE, PPP, HDLC comprehensive experiment
图书馆和档案馆的职能
面试必问 | 一个线程从创建到消亡要经历哪些阶段?
Talk about the metrics of automated testing
[draw rectangular coordinate system in C language]
【你了解Cache吗——全面理解高速缓冲存储器】
TCP three handshakes and four disconnects
Three handshakes and four disconnects of TCP
I wish you a happy Chinese Valentine's day and invite you to read the source code together

![[dimension reduction blow, take you to learn CPU in depth (Part 1)]](/img/59/0c2b8e1a832ef14e2e0b8e8cdcce9c.png)







