当前位置:网站首页>Leetcode-238. product of arrays other than itself
Leetcode-238. product of arrays other than itself
2022-07-25 09:09:00 【KGundam】
Mathematical problems
Topic details
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 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]
Ideas :
Like a brain teaser , The product of each number in the array other than itself is the left multiplied by the right
And how to get the left / The product of all numbers on the right ?
Similar to dynamic programming , We traverse once from left to right , use left Remember the number traversed nums[i] Left product of
namely nums[0]*nums[1]...*nums[i-1]
And it res[i] Multiply and update left Until all the nums[i] Get the corresponding res[i]
( At this time, all the left products are obtained through traversal )
Then use the same method to traverse from right to left , utilize right memory , It's the same thing
Finally, after traversing both sides, we get the desired result
( Note initialization left and right All are 1)
My code :
class Solution
{
public:
vector<int> productExceptSelf(vector<int>& nums)
{
int n = nums.size(), left = 1, right = 1;
vector<int> res(n, 1);
// Left product
for (int i = 0; i < n; ++i)
{
res[i] *= left;
left *= nums[i];
}
// Right product
for (int i = n-1; i >= 0; --i)
{
res[i] *= right;
right *= nums[i];
}
return res;
}
};
We can also combine the two loops into one , To get the following code :
class Solution
{
public:
vector<int> productExceptSelf(vector<int>& nums)
{
int n = nums.size();
int left = 1, right = 1;
vector<int> res(n, 1);
for (int i = 0; i < n; ++i)
{
res[i] *= left;
left *= nums[i];
res[n-1-i] *= right;
right *= nums[n-1-i];
}
return res;
}
};
边栏推荐
猜你喜欢

js弹出式城市筛选组件匹配手机移动端

What are the commands used by pl/sql tools to export SQL files?

LabVIEW experiment - temperature detection system (experimental learning version)

How to avoid duplicate data when the database is high and distributed

为什么说DAO是未来的公司形式
![[STL]stack&queue模拟实现](/img/92/c040c0e937e2666ee179189c60a3f2.png)
[STL]stack&queue模拟实现

Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)

js小游戏源码魔塔闯关下载

Shell脚本
![[deep learning] overview | the latest progress of deep learning](/img/b9/6117862397dcda4d555c819e913c9b.png)
[deep learning] overview | the latest progress of deep learning
随机推荐
Feiling ok1028a core board adapts to rtl8192cu WiFi module
How does Flink SQL persist?
Labview--- signal generator
Fundamentals of C language
优炫数据库对数据的加密是如何做的?
flink sql怎么持久化?
Dark horse programmer JDBC
51 single chip microcomputer key control LED light status
NFT guide for musicians
LeetCode·83双周赛·6129.全0子数组的数目·数学
黑马程序员JDBC
这是我见过写得最烂的Controller层代码...
Arcgis10.2 installation tutorial
Wechat sports ground reservation applet graduation design of applet completion works (2) applet function
这家十年内容产业基建公司,竟是隐形的Web3先行者
【sklearn】sklearn.preprocessing.LabelEncoder
Django4.0 + Web + MySQL5.7 实现简单登录操作
Graduation project of wechat small program ordering system of small program completion works (4) opening report
canvas很多圆圈组成的文本js特效
table表格展开内部行切换效果