当前位置:网站首页>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;
}
};
边栏推荐
- Detailed description of gaussdb (DWS) complex and diverse resource load management methods
- Analysis on the true topic of "cost management" by Guangdong second-class cost engineer
- 不同系统下的文件层级符号小结
- Navigation experiment [microcomputer principle] [experiment]
- leetcode:370. 区间加法
- 直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述
- Liunx instruction
- 实现inotify配合rsync实时备份
- WIN10设置自动拨号联网任务,实现开机、断网自动重连
- GoAhead 翻译—Active Server Pages
猜你喜欢

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

ads131a04 ADC verilog实现及仿真

"Xiaodeng" active directory batch user creation in operation and maintenance

Exit operation in project

透过华为军团看科技之变(五):智慧园区

Storage principle of string

CORDIC based Signal Processor desgn

Practical guide to GStreamer application development (V)
[cloud native practice] kubesphere practice - Multi tenant system practice

「运维有小邓」Active Directory 密码过期通知功能
随机推荐
High energy live broadcast, a gathering of celebrities! We invite you to explore bizdevops.
Three. JS development: drawing of thick lines
PostgreSQL每周新闻—6月22日
一颗新的北极星已经升起!
计算成像前沿进展
高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构
Xiaomi written test real question 1
Shutter bottomnavigationbar with page switching example
Jump to open a new window
直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述
2021 CCPC Harbin J. local minimum (thinking question)
How to judge the quality of conductive slip ring from its appearance
"Xiaodeng" active directory password expiration notification function is available for operation and maintenance
How to call RFC function of ABAP on premises system directly in SAP BTP ABAP programming environment
Analysis of the underlying architecture of spark storage system - spark business environment practice
WPF 测量字符串显示大小
THREEJS基础入门
项目中退出操作
HAproxy + Keepalive实现LDAP代理服务
广东二级造价工程师《造价管理》真题解析