File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number[] }
4
+ */
5
+ var productExceptSelf = function ( nums ) {
6
+ // [-1,1,0,-3,3]
7
+ if ( ! nums . includes ( 0 ) ) {
8
+ const allNumsProduct = nums . reduce ( ( acc , cur ) => acc * cur ) ;
9
+ return nums . map ( ( num ) => allNumsProduct / num ) ;
10
+ } else {
11
+ let arr = [ ] ;
12
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
13
+ if ( nums [ i ] !== 0 ) {
14
+ arr [ i ] = 0 ;
15
+ } else {
16
+ console . log ( "여기는?" ) ;
17
+ const newArr = [ ...nums ] ;
18
+ newArr . splice ( i , 1 ) ;
19
+ console . log ( "newArr ==>" , newArr ) ;
20
+ if ( newArr . includes ( 0 ) ) {
21
+ arr [ i ] = 0 ;
22
+ } else {
23
+ const product = newArr . reduce ( ( arr , cur ) => arr * cur ) ;
24
+ console . log ( "product ==>" , product ) ;
25
+ arr [ i ] = product ;
26
+ }
27
+ }
28
+ }
29
+ return arr ;
30
+ }
31
+ } ;
You can’t perform that action at this time.
0 commit comments