网上做平面设计兼职不错的网站,网站制作 符合百度,全国旅游景点视频大全,个人装修接活app题目描述
给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]A[0]*A[1]*...*A[i-1]*A[i1]*...*A[n-1]。不能使用除法。
解题思路
设有数组大小为5。
对于第一个for循环
第一步#xff1a;b[0] 1;
第二步#xff1a;b[1] b[0] * a[0] a[0]
第三…题目描述
给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]A[0]*A[1]*...*A[i-1]*A[i1]*...*A[n-1]。不能使用除法。
解题思路
设有数组大小为5。
对于第一个for循环
第一步b[0] 1;
第二步b[1] b[0] * a[0] a[0]
第三步b[2] b[1] * a[1] a[0] * a[1];
第四步b[3] b[2] * a[2] a[0] * a[1] * a[2];
第五步b[4] b[3] * a[3] a[0] * a[1] * a[2] * a[3];对于第二个for循环
第一步
temp * a[4] a[4];
b[3] b[3] * temp a[0] * a[1] * a[2] * a[4];第二步
temp * a[3] a[4] * a[3];
b[2] b[2] * temp a[0] * a[1] * a[4] * a[3];第三步
temp * a[2] a[4] * a[3] * a[2];
b[1] b[1] * temp a[0] * a[4] * a[3] * a[2];第四步
temp * a[1] a[4] * a[3] * a[2] * a[1];
b[0] b[0] * temp a[4] * a[3] * a[2] * a[1];代码实现
class Solution {
public:vectorint multiply(const vectorint A) {vectorint res;int length1 A.size();if(length1 0)return res;res.push_back(1);for(int i1;ilength1;i)res.push_back(res[i-1]*A[i-1]);double temp 1;for(int ilength1-1;i0;--i){res[i] res[i]*temp;temp*A[i];}return res;}
};