-
Notifications
You must be signed in to change notification settings - Fork 17
/
Que51.java
73 lines (50 loc) · 1.21 KB
/
Que51.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
import java.util.ArrayList;
/**
* @author: hyl
* @date: 2019/08/17
**/
public class Que51 {
/**
* B[i] = A[0] * ... * A[N-1],除了A[i]变为1
* 时间复杂度为O(N^N)
* @param A
* @return
*/
public int[] multiply(int[] A) {
int[] B = new int[A.length];
for (int i = 0; i < B.length; i++) {
int sum = 1;
for (int j = 0; j < A.length; j++) {
if (i != j){
sum *= A[j];
}
}
B[i] = sum;
}
return B;
}
/**
* 先求出下三角,再求出上三角
* 时间复杂度为O(N)
* @param A
* @return
*/
public int[] multiply1(int[] A) {
int len = A.length;
int[] B = new int[A.length];
if (len != 0){
B[0] = 1;
//计算下三角连乘
for (int i = 0; i < len; i++) {
B[i] = B[i-1] * A[i-1];
}
int temp = 1;
//计算下三角
for (int i = len - 2; i >= 0; --i) {
temp *= A[i+1];
B[i] *= temp;
}
}
return B;
}
}