《剑指offer》第六十六题:构建乘积数组

// 面试题66:构建乘积数组
// 题目:给定一个数组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]。不能使用除法。

#include <cstdio>
#include <vector>

using namespace std;

void BuildProductionArray(const vector<double>& input, vector<double>& output)
{
    //B[i] = ( A[0] * A[1] * ... * A[i-1] ) * ( A[i+1] * ... * A[n-1] )
    //     = C[i] * D[i]
    int length1 = input.size();
    int length2 = output.size();

    if (length1 == length2 && length1 > 1)
    {
        //由上到下计算左下三角矩阵的累计值
        //C[i] = A[0] * A[1] * ... * A[i - 1]
        //     = C[i - 1] * A[i - 1]
        output[0] = 1;
        for (int i = 1; i < length1; ++i)
        {
            output[i] = output[i - 1] * input[i - 1];
        }

        //由下到上计算右上三角矩阵的累计值
        //D[i] = A[i + 1] * A[i + 2] * ... * A[n - 1]
        //     = D[i + 1] * A[i + 1]
        int temp = 1; //保存D[i]值的中间变量
        for (int i = length1 - 2; i >= 0; --i)
        {
            temp = temp * input[i + 1];
            output[i] = output[i] * temp;
        }
    }
}
//================= Test Code =================
static bool EqualArrays(const vector<double>& input, const vector<double>& output)
{
    int length1 = input.size();
    int length2 = output.size();

    if (length1 != length2)
        return false;

    for (int i = 0; i < length1; ++i)
    {
        if (abs(input[i] - output[i]) > 0.0000001)
            return false;
    }

    return true;
}

static void test(const char* testName, const vector<double>& input, vector<double>& output, const vector<double>& expected)
{
    printf("%s Begins: ", testName);

    BuildProductionArray(input, output);
    if (EqualArrays(output, expected))
        printf("Passed.\n");
    else
        printf("FAILED.\n");
}

static void test1()
{
    // 输入数组中没有0
    double input[] = { 1, 2, 3, 4, 5 };
    double output[] = { 0, 0, 0, 0, 0 };
    double expected[] = { 120, 60, 40, 30, 24 };
    vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test1", vector<double>(input, input + sizeof(input) / sizeof(double)),
        ouputTemp,
        vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}

static void test2()
{
    // 输入数组中有一个0
    double input[] = { 1, 2, 0, 4, 5 };
    double output[] = { 0, 0, 0, 0, 0 };
    double expected[] = { 0, 0, 40, 0, 0 };
    vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test2", vector<double>(input, input + sizeof(input) / sizeof(double)),
        ouputTemp,
        vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}

static void test3()
{
    // 输入数组中有两个0
    double input[] = { 1, 2, 0, 4, 0 };
    double output[] = { 0, 0, 0, 0, 0 };
    double expected[] = { 0, 0, 0, 0, 0 };
    vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test3", vector<double>(input, input + sizeof(input) / sizeof(double)),
        ouputTemp,
        vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}

static void test4()
{
    // 输入数组中有正、负数
    double input[] = { 1, -2, 3, -4, 5 };
    double output[] = { 0, 0, 0, 0, 0 };
    double expected[] = { 120, -60, 40, -30, 24 };
    vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test4", vector<double>(input, input + sizeof(input) / sizeof(double)),
        ouputTemp,
        vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}

static void test5()
{
    // 输入输入中只有两个数字
    double input[] = { 1, -2 };
    double output[] = { 0, 0 };
    double expected[] = { -2, 1 };
    vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test5", vector<double>(input, input + sizeof(input) / sizeof(double)),
        ouputTemp,
        vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}

int main(int argc, char* argv[])
{
    test1();
    test2();
    test3();
    test4();
    test5();

    return 0;
}
测试代码

分析:分解计算步骤,单独处理。

class Solution {
public:
    vector<int> multiply(const vector<int>& A) {
        
        vector<int> B;
        int length = A.size();
        if (length < 1)
            return B;
        
        double temp = 1;
        B.push_back(temp);
        for (int i = 1; i < length; ++i)
        {
            temp = B[i - 1] * A[i - 1];
            B.push_back(temp);
        }
        
        temp = 1;
        for (int i = length - 2; i >= 0; --i)
        {
            temp = temp * A[i + 1];
            B[i] = B[i] * temp;
        }
        return B;
    }
};
牛客网提交代码

 

posted @ 2020-04-13 23:52  源周率  阅读(116)  评论(0编辑  收藏  举报