首页 > 代码库 > 面试题52:缺少i的乘积数组

面试题52:缺少i的乘积数组

 

技术分享

 

vector<int> multiply(const vector<int>& A) {    int len = A.size();    vector<int> result(len, 1);    result[0] = 1;    for (int i = 1; i < len; i++)    {        result[i] = result[i - 1] * A[i - 1];    //result[i]保存下三角    }    int tmp = 1;    for (int i = len - 2; i >= 0; i--)    {        tmp = tmp * A[i + 1];    //tmp保存上三角        result[i] *= tmp;    }    return result;}

 

面试题52:缺少i的乘积数组