当前位置:网站首页>[Jianzhi offer] 66 Build product array
[Jianzhi offer] 66 Build product array
2022-07-05 16:58:00 【LuZhouShiLi】
The finger of the sword Offer 66. Building a product array
subject
Given an array A[0,1,…,n-1], Please build an array B[0,1,…,n-1], among B[i] Is the value of an array A In addition to subscript i The product of other elements , namely B[i]=A[0]×A[1]×…×A[i-1]×A[i+1]×…×A[n-1]. Division cannot be used .
Ideas
https://leetcode.cn/problems/gou-jian-cheng-ji-shu-zu-lcof/solution/mian-shi-ti-66-gou-jian-cheng-ji-shu-zu-biao-ge-fe/
- Initializing an array is the same as the original array ,b[0] = 1, Declare an auxiliary variable tmp = 1
- Calculation b[i] The product of the elements of the lower triangle of , Multiply directly into b[i]
- Calculation b[i] The product of the elements of the upper triangle of , Write it down as tmp, And multiply in b[i]
- return b
Code
class Solution {
public:
vector<int> constructArr(vector<int>& a) {
int length = a.size();
if(length == 0)
{
return {
};
}
vector<int> b(length,1); // Declare a length of length Vector The initial value is zero 1
b[0] = 1;
int tmp = 1;
// Calculate the lower triangular matrix from top to bottom
//
for(int i = 1; i < length; i++)
{
b[i] = b[i - 1] * a[i - 1];
}
for(int i = length -2; i >= 0; i--)
{
tmp *= a[i + 1];// You must declare a temporary variable to store Can not be b[i] *= a[i+1]
b[i] *= tmp;
}
return b;
}
};
边栏推荐
- How does win11 change icons for applications? Win11 method of changing icons for applications
- 养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
- JSON转MAP前后数据校验 -- 自定义UDF
- File operation --i/o
- 【剑指 Offer】63. 股票的最大利润
- [echart] resize lodash to realize chart adaptation when window is zoomed
- Copy mode DMA
- Desci: is decentralized science the new trend of Web3.0?
- 齐宣王典故
- 什么是ROM
猜你喜欢
中间表是如何被消灭的?
Cs231n notes (bottom) - applicable to 0 Foundation
Solution of vant tabbar blocking content
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
Practical example of propeller easydl: automatic scratch recognition of industrial parts
File operation --i/o
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
Games101 notes (II)
文件操作--I/O
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
随机推荐
[brush questions] effective Sudoku
Yarn common commands
【剑指 Offer】62. 圆圈中最后剩下的数字
深耕5G,芯讯通持续推动5G应用百花齐放
JSON转MAP前后数据校验 -- 自定义UDF
Games101 notes (II)
阈值同态加密在隐私计算中的应用:解读
Deep dive kotlin synergy (XXI): flow life cycle function
【729. 我的日程安排錶 I】
手机开证券账户安全吗?怎么买股票详细步骤
How does win11 change icons for applications? Win11 method of changing icons for applications
[echart] resize lodash to realize chart adaptation when window is zoomed
C# TCP如何设置心跳数据包,才显得优雅呢?
How to install MySQL
Timestamp strtotime the day before or after the date
BS-XX-042 基于SSM实现人事管理系统
拷贝方式之DMA
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
挖财股票开户安全吗?怎么开股票账户是安全?
How can C TCP set heartbeat packets to be elegant?