[Sword refers to Offer learning] [interview question 52: Building a product array], sword refers to offer
Question: given an array A [0, 1 ,..., N-1], build an array B ,..., N-1], where B [I] = A [0] × A [1] ×... × A [I-1] × A [I + 1] ×... * A [n-1], division is not allowed.Solutions
Example: A [] = {1, 2, 3} evaluate B []
B [0] = A [1] × A [2] = 2 × 3 = 6
B [1] = A [0] × A [2] = 1 × 3 = 3
B [2] = A [0] × A [1] = 1 × 2 = 2
1. B [0] is initialized to 1. Starting from subscript I = 1, first obtain the value of C [I] and put it into B [I], that is, B [I] = C [I] = C [I-1] × A [I-1], therefore, B [1] = B [1-1] × A [1-1] = B [0] × A [0] = 1 × 1 = 1, I ++
2. B [2] = B [2-1] × A [2-1] = B [1] × A [1] = 1 × 2 = 2, I ++ has exceeded the length to stop the loop.
3. C [I] After computation, calculate D [I] and set a temporary variable temp to 1.
4. From the back to the variable array, LengthA = 3 initialize I = LengthA-2 = 1, the end condition is I> = 0
5. in the first cycle, temp = temp × A [I + 1] = 1 × A [2] = 3, calculate the value of the last element in A and put it in temp, temp is equivalent to D [I ].
6. because the previous B [I] = C [I], let B [I] × D [I] be the result to be saved, that is, B [I] = B [1] = B [1] × temp = 1 × 3 = 3, I-= 0
7. calculate B [I] = B [0]. The value of temp in the previous step is A [2]. in this cycle, temp = temp × A [0 + 1] = A [2] × A [1] = 3 × 2 = 6
8. B [I] = B [0] = B0] × temp = 1 × 6 = 6, I-<0 loop ends
Therefore, array B is {6, 3, 2}
Code Implementation
Import java. util. arrays; public class Test52 {public static double [] multiply (double [] data) {if (data = null | data. length <2) {return null;} double [] result = new double [data. length]; // result [0] Take 1 result [0] = 1; for (int I = 1; I <data. length; I ++) {// In the first step, each result [I] is equal to data [0] * data [1]... data [I-1] // when I = n-1, now the result [n-1] has been calculated [A] result [I] = result [I-1] * data [I-1];} // tmp save data [n-1] * data [N-2]... result of data [I + 1] double tmp = 1; // The second step is data [n-1] * data [N-2]... data [I + 1] // [A] result [n-1] has been calculated. length-2 start to operate for (int I = data. length-2; I> = 0; I --) {tmp * = data [I + 1]; result [I] * = tmp;} return result ;} public static void main (String [] args) {double [] array1 = {1, 2, 3, 4, 5}; System. out. println (Arrays. toString (multiply (array1); // double expected [] ={ 120, 60, 40, 30, 24}; double [] array2 = {1, 2, 0, 4, 5}; System. out. println (Arrays. toString (multiply (array2); // double expected [] = {0, 0, 40, 0, 0}; double [] array3 = {1, 2, 0, 4, 0}; System. out. println (Arrays. toString (multiply (array3); // double expected [] = {0, 0, 0, 0}; double [] array4 = {1,-2, 3,-4, 5}; System. out. println (Arrays. toString (multiply (array4); // double expected [] ={ 120,-60, 40,-30, 24}; double [] array5 = {1, -2}; System. out. println (Arrays. toString (multiply (array5); // double expected [] = {-2, 1 };}}
Running result
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.