标签:amp inf multi img ++ tor i++ == 除法
给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
class Solution { public: vector<int> multiply(const vector<int>& A) { int n = A.size(); vector<int> B; if(n == 0)return B; B[0] = 1; for(int i = 1; i < n; i++){ B[i] = B[i-1] * A[i-1]; } int temp = 1; for(int j = n-2; j >= 0; j-- ){ temp *= A[j+1]; B[j] *= temp; } return B; } };
标签:amp inf multi img ++ tor i++ == 除法
原文地址:https://www.cnblogs.com/icehole/p/12141933.html