-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode238.java
More file actions
28 lines (26 loc) · 810 Bytes
/
leetcode238.java
File metadata and controls
28 lines (26 loc) · 810 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
public class leetcode238 {
public static int[] productExceptSelf(int[] nums) {
int[] suff = new int[nums.length];
int[] pref = new int[nums.length];
int[] answer = new int[nums.length];
suff[nums.length-1]=1;
pref[0]=1;
for(int i = nums.length-2; i>=0; i--){
suff[i] = suff[i+1]*nums[i+1];
}
for(int i = 1; i< nums.length;i++){
pref[i] = pref[i-1]*nums[i-1];
}
for(int i = 0; i< nums.length;i++){
answer[i] = suff[i]*pref[i];
}
return answer;
}
public static void main(String[] args) {
int[] nums = {1,2,3,4};
int[]res = productExceptSelf(nums);
for(int i =0; i<nums.length ; i++){
System.out.println(res[i]);
}
}
}