Skip to content

Commit b9dce6a

Browse files
committed
week2-3
1 parent 9666671 commit b9dce6a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int[] answer = new int[nums.length];
4+
int[] front = new int[nums.length];
5+
int[] back = new int[nums.length];
6+
7+
for (int i = 0; i < nums.length; i++) {
8+
front[i] = 1;
9+
back[i] = 1;
10+
}
11+
12+
for (int i = 0; i < nums.length - 1; i++) {
13+
front[i + 1] = front[i] * nums[i];
14+
}
15+
16+
for (int i = nums.length - 1; i > 0; i--) {
17+
back[i - 1] = nums[i] * back[i];
18+
}
19+
20+
for (int i = 0; i < nums.length; i++) {
21+
answer[i] = front[i] * back[i];
22+
}
23+
24+
return answer;
25+
}
26+
}

0 commit comments

Comments
 (0)