Skip to content

Commit 906ec0e

Browse files
committed
238. Product of Array Except Self
1 parent af4f23c commit 906ec0e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
4+
// 원래는 그냥 다 곱해서 자기 빼고 나누면 되는데 나눗셈 하지 말라고 함
5+
6+
// 왼쪽 곱 구한다음에 오른쪽 곱으로 리턴하면 됨
7+
int[] answer = new int[nums.length];
8+
9+
int left = 1;
10+
answer[0] = left;
11+
12+
for (int i = 1; i < nums.length; i++) {
13+
answer[i] = answer[i - 1] * nums[i - 1];
14+
}
15+
16+
// 1 1 2 6
17+
18+
// 오른쪽 누적 곱
19+
20+
int right = 1;
21+
22+
for (int i = nums.length - 1; i >= 0; i--) {
23+
answer[i] = answer[i] * right;
24+
right *= nums[i];
25+
}
26+
27+
28+
return answer;
29+
30+
}
31+
}

0 commit comments

Comments
 (0)