We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 91f6187 commit 25693dfCopy full SHA for 25693df
product-of-array-except-self/JEONGBEOMKO.java
@@ -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