当前位置:网站首页>Leetcode skimming -- no.238 -- product of arrays other than itself
Leetcode skimming -- no.238 -- product of arrays other than itself
2022-07-27 02:40:00 【Life is made by oneself ~】
The product of an array other than itself
One 、 Title Description
Give you an array of integers nums, return Array answer , among answer[i] be equal to nums Middle Division nums[i] Product of other elements .
Subject data Guarantee Array nums The product of all prefix elements and suffixes of any element in 32 position In the range of integers .
Please do not use division , And in O(n) Complete this question in time complexity .
Example :
Example 1:
Input : nums = [1,2,3,4]
Output : [24,12,8,6]
Example 2:
Input : nums = [-1,1,0,-3,3]
Output : [0,0,9,0,0]
Advanced : You can O(1) Do you complete this problem within the additional spatial complexity of ?( For the purpose of spatial complexity analysis , Output arrays are not treated as extra space .)
Two 、 Thought analysis
Product left and right list
1) Create two sizes with nums Same array l and r ,l[i] representative i The product of all numbers on the left ,r[i] representative i Product of all numbers on the right .
2) Fill two arrays , Yes l Yes :l[i] = l[i - 1] * nums[i - 1]; Here we should pay attention to setting the leftmost number to 1:l[0] = 1; Yes r Yes :r[i] = r[i + 1] * nums[i + 1]; Here we should pay attention to setting the rightmost number to 1:r[numsSize - 1] = 1;
3) Finally, the value of each position is :r[i] * l[i];
Pictured :
The same is true for the right side
3、 ... and 、 Code
/** * 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;
}
Four 、 Advanced
Although the above method solves the problem , However, the spatial complexity is not O(1). by O(N)(nums Size )
Here's a point : The output array is not included in the space complexity .
So we just need to create l Array , Create a variable directly from right to left R Instead of ,R Multiplicative multiplication nums The numerical :
/** * 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;
}
边栏推荐
- Static routing experiment configuration
- 小程序utils
- [draw sherpinski triangle in C language]
- 蚂蚁京东新浪10位架构师424页佳作深入分布式缓存从原理到实践pdf
- JVM interview questions (necessary for interview)
- The problem of storing elements in TreeSet collection
- Record the star user of handsomeblog
- N methods of SQL optimization
- cookie增删改查方法
- I wish you a happy Chinese Valentine's day and invite you to read the source code together
猜你喜欢
![[C language] relevant distinction between strlen and sizeof](/img/c0/c026818692a01c1867771434e90da8.png)
[C language] relevant distinction between strlen and sizeof
![文章主要内容提取软件[基于NLP技术]](/img/1c/7c1b0e9bc9af62308f4124104f6110.png)
文章主要内容提取软件[基于NLP技术]

最新多线程&高并发学习资料,面试心里有底气

函数栈帧详解

【用C语言绘制直角坐标系】
![[draw sherpinski triangle in C language]](/img/e6/9d1d088d1c7675c23725443000329b.png)
[draw sherpinski triangle in C language]

【降维打击,带你深度学习CPU(上)】
![[brother Yang takes you to play with the linear table (III) - two way linked list]](/img/64/5367ff4fb6797565cb1f9e1a8aee4e.png)
[brother Yang takes you to play with the linear table (III) - two way linked list]

OSPF routing information protocol topology experiment

Redis安装及运行(linux)
随机推荐
毕业进入HW,从测试工程师到项目经理,现如今在鹅厂年收入百万,我的给大家的一些建议...
Hcip day 6 OSPF static experiment
Hcip the next day
Little sister's notes: how do I learn simple source code to expand my horizons
The pointer is really profound!!!
Is it useful to lie down with your eyes closed when you can't sleep?
Uni app wechat applet search keywords are displayed in red
项目时区问题解决
Guangguangzai's CSDN journey
QT Chinese garbled constant newline ultimate solution
Sort the three integers from large to small (introduce various methods in detail)
C语言 学生信息管理系统 基于数组 可以存取到文本文件
【Redis】五种常用的数据类型
Record the nth SQL exception
用swiper分类图标
【无标题】
[brother Yang takes you to play with the linear table (III) - two way linked list]
测试工作十年,想对还在迷茫的朋友说:一点要做好个人规划...
[use SQLite3 library to realize student information management system in Visual Studio 2019]
uni-app 微信小程序搜索关键字标红显示