Array Construction The product offer prove safety

Title Description

Given an array A [0,1, ..., n- 1], please construct an array B [0,1, ..., n- 1], where B is the element B [I] = A [0] A [ . 1] ... A * [. 1-I] A [I +. 1] ... * A [n--. 1]. You can not use the division. (Note: The predetermined B [0] = A [1 ] * A [2] * ... * A [n-1], B [n-1] = A [0] * A [1] * ... * A [n -2];)

Thinking

  1. An array of n points obtained in Step B, the time step k, in addition to B [k] In addition, all the items of B by A [i]

Code

method one:

class Solution {
public:
    vector<int> multiply(const vector<int>& A) {
        vector<int> res_zero;
        if(A.size() == 0)
            return res_zero;
        vector<int> res(A.size(),1);
        for(int i = 0; i < A.size();i++)
        {
            for(int j = 0; j < A.size(); j++)
            {
                if(j != i)
                {
                    res[j] = res[j] * A[i];
                }
            }
        }
        return res;
    }
};
Published 85 original articles · won praise 0 · Views 406

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/104744990