当前位置:网站首页>leetcode:238. 除自身以外数组的乘积
leetcode:238. 除自身以外数组的乘积
2022-06-29 20:51:00 【OceanStar的学习笔记】
题目来源
题目解析

前缀和
根据题意,对于每个 a n s [ i ] ans[i] ans[i]均由两部分组成:
( 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])
所以,我们可以用前缀和的思想,申请两个数组 s 1 [ ? ] , s 2 [ ? ] s1[?],s2[?] s1[?],s2[?] (假设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]
处理完成之后: 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] 为索引 i 左侧所有元素的乘积
s1[0] = 1;
for (int i = 1; i < m; ++i) {
s1[i] = s1[i - 1] * nums[i - 1];
}
// s2[i] 为索引 i 右侧所有元素的乘积
s2[m - 1] = 1;
for (int i = m - 2; i >= 0; --i) {
s2[i] = s2[i + 1] * nums[i + 1];
}
// 对于索引 i,除 nums[i] 之外其余各元素的乘积就是左侧所有元素的乘积乘以右侧所有元素的乘积
vector<int> ans(m, 0);
for (int i = 0; i < m; ++i) {
ans[i] = s1[i] * s2[i];
}
return ans;
}
};

空间复杂度 O(1)O(1) 的方法
由于输出数组不算在空间复杂度内,那么我们可以将 L 或 R 数组用输出数组来计算。先把输出数组当作 L 数组来计算,然后再动态构造 R 数组得到结果。
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;
}
};
边栏推荐
- .NetCore统一认证授权学习——第一次授权(2)
- Mapbox GL development tutorial (12): loading surface layer data
- Alibaba cloud released the atlas of China's robot industry (2022), 122 Pages pdf
- Exit operation in project
- 日本樱桃一颗拍出1980元天价,网友:吃了有上当的感觉
- Bigder: Automation Test Engineer
- What are the mainstream brands of smart door locks? What characteristics should we pay attention to when purchasing door locks?
- Golang basic learning
- 导航【微机原理】
- Community interview -- jumpserver open source fortress in the eyes of an it newcomer
猜你喜欢

mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总

WIN10设置自动拨号联网任务,实现开机、断网自动重连
![Navigation exercises [microcomputer principles] [exercises]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation exercises [microcomputer principles] [exercises]
![[compilation principle] semantic analysis](/img/2e/9c17da3dbc758b2985e55201c73f99.png)
[compilation principle] semantic analysis

Chainsafe cross chain bridge deployment tutorial

Hangfire details

Logical structure and physical structure

18. `bs object Node name next_ sibling` previous_ Sibling get sibling node

Bigder:自动化测试工程师

Mapbox GL development tutorial (12): loading surface layer data
随机推荐
2021 CCPC Harbin J. local minimum (thinking question)
How do I audit Active Directory User account changes?
导航 习题【微机原理】【习题】
Jupyter service installation and startup
I found another cross domain method by chance. I don't know if anyone has ever played this way
计算成像前沿进展
Liunx instruction
MySQL JSON data types & functions
Information system project manager -- Chapter VII examination questions of project cost management over the years
如何评价科大讯飞AI翻译笔P20系列,值得买吗?
60天远程办公经验分享 | 社区征文
Jupyter服务安装及启动
分析影响导电滑环传输信号的因素
2021 CCPC Harbin E. power and modulo (thinking questions)
Practical guide to GStreamer application development (V)
Reinforcement learning weekly (issue 51): integration of PAC, ilql, RRL & model free reinforcement learning into micro grid control: overview and Enlightenment
Gstreamer应用开发实战指南(五)
「运维有小邓」Active Directory批量用户创建
At least 3 years for learning amplifier?
「运维有小邓」实时监控用户登录操作