Given an array a [n], we want to construct array B [N], where B [I] = A [0] * A [1] *... * A [N-1]/A [I]. In the construction process:
Division is not allowed;
Requires O (1) space complexity and O (n) time complexity;
In addition to traversing the counter and a [n] B [N], new variables (including stack temporary variables, space and global static variables) cannot be used );
UseProgramImplementation and simple description.
First look at the implementationCode:
Void product (int A [], int B [], int N) {B [0] = 1; for (INT I = 1; I <n; I ++) B [I] = B [I-1] * A [I-1]; for (INT I = N-1; I> = 1; I --) {B [I] = B [I] * B [0]; B [0] = B [0] * A [I];} return ;}
In the first loop, for each loop I, B [I] saves a [0] * A [1] *... * A [I-1].
In the second loop, for each loop I, start B [0] to save a [I + 1] * A [I + 2] *... * A [N-1], B [I] = B [I] * B [0] is ( A [0] * A [1] *... * A [I-1]) * (a [I + 1] * A [I + 2] *... * A [N-1]), updating B [0] at the same time. By the end of the loop, B [0] is also a [1] * A [2] *... * A [N-1].