/
105_ConstuctArray.java
85 lines (68 loc) · 2.4 KB
/
105_ConstuctArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Coding Interviews: Questions, Analysis & Solutions
// Harry He
public class ConstuctArray {
public static void multiply(double array1[], double array2[]){
if(array1.length == array2.length && array1.length > 0){
array2[0] = 1;
for(int i = 1; i < array1.length; ++i){
array2[i] = array2[i - 1] * array1[i - 1];
}
int temp = 1;
for(int i = array1.length - 2; i >= 0; --i){
temp *= array1[i + 1];
array2[i] *= temp;
}
}
}
//================= Test Code =================
private static boolean equalArrays(double array1[], double array2[]){
if(array1.length != array2.length)
return false;
for(int i = 0; i < array1.length; ++i){
if(abs(array1[i] - array2[i]) > 0.0000001)
return false;
}
return true;
}
private static double abs(double d) {
return (d > 0) ? d : -d;
}
private static void test(String testName, double array1[], double array2[], double expected[]){
System.out.print(testName + " Begins: ");
multiply(array1, array2);
if(equalArrays(array2, expected))
System.out.println("Passed.");
else
System.out.println("FAILED.");
}
private static void test1(){
double array1[] = {1, 2, 3, 4, 5};
double array2[] = new double[5];
double expected[] = {120, 60, 40, 30, 24};
test("Test1", array1, array2, expected);
}
private static void test2(){
double array1[] = {1, 2, 0, 4, 5};
double array2[] = new double[5];
double expected[] = {0, 0, 40, 0, 0};
test("Test2", array1, array2, expected);
}
private static void test3(){
double array1[] = {1, 2, 0, 4, 0};
double array2[] = new double[5];
double expected[] = {0, 0, 0, 0, 0};
test("Test3", array1, array2, expected);
}
private static void test4(){
double array1[] = {1, -2, 3, -4, 5};
double array2[] = new double[5];
double expected[] = {120, -60, 40, -30, 24};
test("Test4", array1, array2, expected);
}
public static void main(String[] args){
test1();
test2();
test3();
test4();
}
}