File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number[][] }
4
+ */
5
+ var threeSum = function ( nums ) {
6
+ const result = [ ] ;
7
+ const numsMap = { } ;
8
+
9
+ nums . forEach ( ( num , index ) => {
10
+ numsMap [ num ] = ( numsMap [ num ] || 0 ) + 1 ;
11
+
12
+ } ) ;
13
+
14
+ for ( let i = 0 ; i < nums . length - 1 ; i ++ ) {
15
+ for ( let j = i + 1 ; j < nums . length ; j ++ ) {
16
+ const a = nums [ i ] ;
17
+ const b = nums [ j ] ;
18
+ const targetNum = - ( a + b ) ;
19
+
20
+ if ( ! numsMap [ targetNum ] ) {
21
+ continue ;
22
+ }
23
+
24
+ if ( ( targetNum === a || targetNum === b ) && numsMap [ targetNum ] === 1 ) {
25
+ continue ;
26
+ }
27
+
28
+ if ( targetNum === a && targetNum === b && numsMap [ targetNum ] <= 2 ) {
29
+ continue ;
30
+ }
31
+
32
+ const triplet = [ a , b , targetNum ] . sort ( ( x , y ) => x - y ) ;
33
+ const key = triplet . join ( ',' ) ;
34
+
35
+ if ( seen . has ( key ) ) {
36
+ continue ;
37
+ }
38
+
39
+ seen . add ( key ) ;
40
+
41
+ result . push ( triplet ) ;
42
+ }
43
+ }
44
+
45
+ return result ;
46
+ } ;
47
+
48
+
49
+ //์๊ฐ ๋ณต์ก๋ O(nยฒ) , ๊ณต๊ฐ ๋ณต์ก๋ O(nยฒ) ๋ผ๊ณ ์๊ฐํ๋๋ฐ Time Limit Exceeded ์๋ฌ ๋ฐ์
50
+ // ์ถํ ๋ค์ ํ์ด๋ณผ ์์
You canโt perform that action at this time.
0 commit comments