当前位置:网站首页>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;
}
边栏推荐
- hcip--ospf接口网络接口类型实验
- 中断、信号、系统调用
- 证券公司哪家手续费最低?手机上开户安全吗
- What is the principle of synchronized lock escalation in multithreading?
- 想要彻底搞的性能优化,得先从底层逻辑开始了解~
- RIP路由信息协议-拓扑实验
- Area optimization of digital chips: detailed explanation of question 1 in the digital direction of the third "Huawei Cup" graduate innovation core competition
- Risc-v tool chain compilation notes
- 最新多线程&高并发学习资料,面试心里有底气
- 小程序utils
猜你喜欢

Hcip day 6 OSPF static experiment

Record the star user of handsomeblog

中断、信号、系统调用
![[draw sherpinski triangle in C language]](/img/e6/9d1d088d1c7675c23725443000329b.png)
[draw sherpinski triangle in C language]

C language student information management system can access text files based on arrays

Solve prime numbers between 100 and 200

OSPF routing information protocol topology experiment

函数栈帧详解

I wish you a happy Chinese Valentine's day and invite you to read the source code together

After ten years of testing, I want to say to my friends who are still confused: one thing is to do a good job in personal planning
随机推荐
BigDecimal 的 4 个坑,你踩过几个?
静态路由实验配置
C语言 学生信息管理系统 基于数组 可以存取到文本文件
通达信开户安全么
【洋哥带你玩转线性表(三)——双向链表】
As for the pit saved by serialized variables, the data added with indexer cannot be serialized
Is it useful to lie down with your eyes closed when you can't sleep?
Fist guessing applet based on Object-C novice on the road
Hcip day 4 OSPF routing protocol
F8 catch traffic, F9 catch rabbits, f10turttle
想要彻底搞的性能优化,得先从底层逻辑开始了解~
猜拳小程序 基于Object-C 新手上路
After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling
FormData的使用
Talk about the metrics of automated testing
N methods of SQL optimization
【斐波那契数列及螺线 基于C语言】
[dimension reduction blow, take you to learn CPU in depth (Part 1)]
Static routing experiment configuration
【洋哥带你玩转线性表(四)——链式队列】