File tree 1 file changed +18
-19
lines changed
product-of-array-except-self
1 file changed +18
-19
lines changed Original file line number Diff line number Diff line change @@ -26,23 +26,22 @@ function applyRightProducts(nums: number[], result: number[]) {
26
26
27
27
// 2번 풀이
28
28
function productExceptSelf2 ( nums : number [ ] ) : number [ ] {
29
- const length = nums . length ;
30
- const result : number [ ] = new Array ( length ) . fill ( 1 ) ;
31
-
32
- // 1단계: 왼쪽 누적 곱 저장
33
- let prefix = 1 ;
34
- for ( let i = 0 ; i < length ; i ++ ) {
35
- result [ i ] = prefix ;
36
- prefix *= nums [ i ] ;
37
- }
38
-
39
- // 2단계: 오른쪽 누적 곱 곱하기 (in-place)
40
- let suffix = 1 ;
41
- for ( let i = length - 1 ; i >= 0 ; i -- ) {
42
- result [ i ] *= suffix ;
43
- suffix *= nums [ i ] ;
44
- }
45
-
46
- return result ;
29
+ const length = nums . length ;
30
+ const result : number [ ] = new Array ( length ) . fill ( 1 ) ;
31
+
32
+ // 1단계: 왼쪽 누적 곱 저장
33
+ let prefix = 1 ;
34
+ for ( let i = 0 ; i < length ; i ++ ) {
35
+ result [ i ] = prefix ;
36
+ prefix *= nums [ i ] ;
37
+ }
38
+
39
+ // 2단계: 오른쪽 누적 곱 곱하기 (in-place)
40
+ let suffix = 1 ;
41
+ for ( let i = length - 1 ; i >= 0 ; i -- ) {
42
+ result [ i ] *= suffix ;
43
+ suffix *= nums [ i ] ;
47
44
}
48
-
45
+
46
+ return result ;
47
+ } ;
You can’t perform that action at this time.
0 commit comments