当前位置:网站首页>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;
}
};
边栏推荐
- How to do the game plug-in?
- YOLOV5环境配置
- 附加:在在下部分区/县(数据表)
- The development of art NFT
- What is steel grating?
- [STL]stack&queue模拟实现
- C语言实现二叉平衡树
- PHP gets all child nodes under any parent node of the tree structure
- Additional: SQL statement area / county in the lower half (data table)
- Visual query (sp_helptext) -- quick query of stored procedures containing specified strings (with source code)
猜你喜欢

canvas动态图片头像晃动js特效

This is the worst controller layer code I've ever seen

Robot jumping problem

神经网络学习(1)前言介绍

How to do the game plug-in?

The operation cannot be completed because a folder or file in it is already open in another program

Arrange the array into the smallest number

YOLOV5环境配置

Redis学习笔记

How to choose a low code software development platform?
随机推荐
整理 华为AP-3010DN_V2配置创建wifi
The garbage classification data set used in the excellent Yolo target detection training is shared - about 3000 labeled
[Development Tutorial 9] crazy shell · open source Bluetooth smart health watch - storage
js弹出式城市筛选组件匹配手机移动端
Wechat sports ground reservation applet graduation project of applet completion works (1) development outline
Wechat Reservation Reservation of applet completion works applet graduation project (8) graduation project thesis template
Robot jumping problem
Wechat reservation of small program completion works (5) assignment book of small program graduation project
Practice of establishing dynamic programming state transition equation
Graduation design of wechat small program ordering system of small program completion works (3) background function
js触屏小游戏源码冰雪之旅
sql注入
How to use pixi.js to make simple Parkour games
Wechat reservation of completed works of applet graduation project (4) opening report
What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions
[hero planet July training leetcode problem solving daily] 19th binary tree
What is steel grating?
(self drawn ugly picture) simple understanding tcp/ip three handshakes and four waves
The development of art NFT
flink sql怎么持久化?