题解 | #构建乘积数组#
构建乘积数组
https://www.nowcoder.com/practice/94a4d381a68b47b7a8bed86f2975db46
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ export function multiply(A: number[]): number[] { // write code here const ascMul = [A[0]] const descMul = [A[A.length - 1]] for (let i = 1; i < A.length; i++) { ascMul[i] = ascMul[i - 1] * A[i] } for (let i = 1; i < A.length; i++) { descMul[i] = descMul[i - 1] * A[A.length - i - 1] } const B = [] B[0] = descMul[A.length - 2] B[A.length - 1] = ascMul[A.length - 2] for (let i = 1; i < A.length - 1; i++) { B[i] = ascMul[i - 1] * descMul[A.length - i - 2] } return B }
O(N)
O(N)