Skip to content

Commit 326987e

Browse files
committed
product of array except self solution
1 parent 23f97a9 commit 326987e

File tree

1 file changed

+23
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)