当前位置:网站首页>[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;
}
};
边栏推荐
- Oneforall installation and use
- Jarvis OJ Telnet Protocol
- Combined use of vant popup+ other components and pit avoidance Guide
- What is ROM
- The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
- File operation --i/o
- C how TCP restricts the access traffic of a single client
- 【729. 我的日程安排錶 I】
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
- Jarvis OJ shell traffic analysis
猜你喜欢
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
【刷题篇】有效的数独
深耕5G,芯讯通持续推动5G应用百花齐放
How was the middle table destroyed?
Hiengine: comparable to the local cloud native memory database engine
[729. My schedule I]
Etcd build a highly available etcd cluster
Seaborn draws 11 histograms
有序链表集合求交集 方法 总结
随机推荐
Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
如何将mysql卸载干净
DenseNet
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
Iphone14 with pill screen may trigger a rush for Chinese consumers
深潜Kotlin协程(二十一):Flow 生命周期函数
麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
數據訪問 - EntityFramework集成
清晰还原31年前现场,火山引擎超清修复Beyond经典演唱会
composer安装报错:No composer.lock file present.
Games101 notes (III)
Writing method of twig array merging
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
【刷题篇】有效的数独
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
数据访问 - EntityFramework集成
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
【729. 我的日程安排表 I】
Jarvis OJ Webshell分析
Jarvis OJ Flag