Skip to content

Commit 25693df

Browse files
committed
add: product of array except self solution
1 parent 91f6187 commit 25693df

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* [time-complexity]: O(n)
2+
[space-complexity]: O(1)
3+
*/
4+
public class JEONGBEOMKO {
5+
6+
class Solution {
7+
public int[] productExceptSelf(int[] nums) {
8+
int n = nums.length;
9+
int[] result = new int[n];
10+
11+
// 왼쪽 곱 계산
12+
result[0] = 1;
13+
for (int i = 1; i < n; i++) {
14+
result[i] = result[i - 1] * nums[i - 1];
15+
}
16+
17+
// 오른쪽 곱과 동시에 결과 계산
18+
int rightProduct = 1;
19+
for (int i = n - 1; i >= 0; i--) {
20+
result[i] = result[i] * rightProduct;
21+
rightProduct *= nums[i];
22+
}
23+
24+
return result;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)