-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductExceptSelf.java
More file actions
41 lines (28 loc) · 910 Bytes
/
ProductExceptSelf.java
File metadata and controls
41 lines (28 loc) · 910 Bytes
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
import java.util.Arrays;
public class ProductExceptSelf {
public static void main(String[] args) {
// Input: nums = [-1,1,0,-3,3]
// Output: [0,0,9,0,0]
int[] array = {-1,1,0,-3,3};
int[] ans = productExceptSelf(array);
System.out.println(Arrays.toString(ans));
}
public static int[] productExceptSelf(int[] nums) {
int[] TempArray = new int[nums.length];
for (int i = 0; i < TempArray.length; i++) {
TempArray[i]=1;
for (int j = 0; j < TempArray.length; j++) {
if(i==j){
continue;
}
else{
TempArray[i] = TempArray[i] * nums[j];
}
}
// if(nums[i]!=0){
// TempArray[i] /= nums[i];
// }
}
return TempArray;
}
}