Skip to content

Commit 7aa7393

Browse files
committed
product-of-array-except-self solution
1 parent 5fe4ef9 commit 7aa7393

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class kimjunyoung90 {
2+
public int[] productExceptSelf(int[] nums) {
3+
int[] answers = new int[nums.length];
4+
5+
//왼쪽 값 계산
6+
answers[0] = 1;
7+
for (int i = 1; i < nums.length; i++) {
8+
answers[i] = answers[i - 1] * nums[i - 1];
9+
}
10+
11+
//오른쪽 값 계산
12+
int right = 1;
13+
for (int i = nums.length - 1; i >= 0; i--) {
14+
answers[i] *= right;
15+
//다음 값 right 계산
16+
right *= nums[i];
17+
}
18+
return answers;
19+
}
20+
}

0 commit comments

Comments
 (0)