当前位置:网站首页>leetcode:238. Product of arrays other than itself
leetcode:238. Product of arrays other than itself
2022-06-29 20:56:00 【Oceanstar's learning notes】
Title source
title

The prefix and
According to the meaning , For each a n s [ i ] ans[i] ans[i] Both consist of two parts :
( n u m s [ 0 ] × n u m s [ 1 ] × . . . × n u m s [ i − 1 ] ) × ( n u m s [ i + 1 ] × n u m s [ i + 2 ] × . . . × n u m s [ n − 1 ] ) (nums[0]×nums[1]×...×nums[i−1])×(nums[i+1]×nums[i+2]×...×nums[n−1]) (nums[0]×nums[1]×...×nums[i−1])×(nums[i+1]×nums[i+2]×...×nums[n−1])
therefore , We can use the idea of prefix and , Request two arrays s 1 [ ? ] , s 2 [ ? ] s1[?],s2[?] s1[?],s2[?] ( hypothesis n = nums.size())
- s 1 [ i ] = n u m [ 0..... i − 1 ] s1[i] = num[0.....i-1] s1[i]=num[0.....i−1]
- s 2 [ i ] = n u m [ i + 1.... n − 1 ] s2[i] = num[i+1....n-1] s2[i]=num[i+1....n−1]
After processing : a n s [ i ] = s 1 [ i ] ∗ s 2 [ i ] ans[i] = s1[i] * s2[i] ans[i]=s1[i]∗s2[i]


class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
if(nums.empty()){
return {
};
}
int m = nums.size();
std::vector<int> s1(m, 0), s2(m, 0);
// s1[i] Index i The product of all the elements on the left
s1[0] = 1;
for (int i = 1; i < m; ++i) {
s1[i] = s1[i - 1] * nums[i - 1];
}
// s2[i] Index i The product of all elements on the right
s2[m - 1] = 1;
for (int i = m - 2; i >= 0; --i) {
s2[i] = s2[i + 1] * nums[i + 1];
}
// For index i, except nums[i] The product of other elements is the product of all elements on the left multiplied by the product of all elements on the right
vector<int> ans(m, 0);
for (int i = 0; i < m; ++i) {
ans[i] = s1[i] * s2[i];
}
return ans;
}
};

Spatial complexity O(1)O(1) Methods
Because the output array is not included in the space complexity , Then we can L or R Arrays are computed using output arrays . First treat the output array as L Array to calculate , And then dynamically construct R Array gets the result .
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
if(nums.empty()){
return {
};
}
int m = nums.size();
int pre = 0, front = 0;
vector<int> ans(m, 1);
pre = 1;
ans[0] *= pre;
for (int i = 1; i < m; ++i) {
pre = pre * nums[i - 1];
ans[i] *= pre;
}
front = 1;
ans[m - 1] *= front;
for (int i = m - 2; i >= 0; --i) {
front = front * nums[i + 1];
ans[i] *= front;
}
return ans;
}
};
边栏推荐
- Deploy web using gunicorn Py application
- Basic qualities of management personnel
- Chainsafe cross chain bridge deployment tutorial
- 透过华为军团看科技之变(五):智慧园区
- Navigation [microcomputer principle]
- 推荐书籍--白夜行
- 直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述
- Analysis on the true topic of "cost management" by Guangdong second-class cost engineer
- Analysis of the underlying architecture of spark storage system - spark business environment practice
- Live broadcast preview | PostgreSQL kernel Interpretation Series Lecture 1: overview of PostgreSQL system
猜你喜欢

Coreldraw2022 new version v24.1.0.360 update

双目立体视觉摄像头的标定、矫正、世界坐标计算(opencv)

Explain PBR texture maps

Verilog implements DDS waveform generator module, which can realize adjustable frequency and phase, three waveforms

Cantata version 9.5 has officially passed the sgs-t Ü V certification and conforms to all major software safety standards

不同系统下的文件层级符号小结

How do I audit Active Directory User account changes?

Exercise 8 Chapter 8 Verilog finite state machine design -4 Verilog quartus Modelsim
【云原生实战】KubeSphere实战——多租户系统实战

路由汇总带来的三层环路-解决实验
随机推荐
广东二级造价工程师《造价管理》真题解析
"Xiaodeng" active directory password expiration notification function is available for operation and maintenance
管理人员应具备的基本素质
What are the mainstream brands of smart door locks? What characteristics should we pay attention to when purchasing door locks?
How to judge the quality of conductive slip ring from its appearance
CORDIC based Signal Processor desgn
THREEJS基础入门
How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native vs traditional technology architecture
A new Polaris has risen!
路由汇总带来的三层环路-解决实验
WPF 测量字符串显示大小
Leading by 11%, Huawei cloud sky chip AI solver once again topped the international authoritative list
高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构
LSF-bsub命令
String类的常用方法
PostgreSQL Weekly News - 22 juin
Is it safe to open an account with flush for stock trading?
量子机器学习的基础和应用:一个简明文献综述
CORDIC based Signal Processor desgn
Reinforcement learning weekly (issue 51): integration of PAC, ilql, RRL & model free reinforcement learning into micro grid control: overview and Enlightenment