File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number[][] }
4
+ */
5
+
6
+ // 중복 제거 중점..
7
+ var threeSum = function ( nums ) {
8
+ nums . sort ( ( a , b ) => a - b ) ;
9
+ const result = [ ] ;
10
+
11
+ for ( let i = 0 ; i < nums . length - 2 ; i ++ ) {
12
+ if ( i > 0 && nums [ i ] === nums [ i - 1 ] ) continue ;
13
+
14
+ let left = i + 1 ;
15
+ let right = nums . length - 1 ;
16
+
17
+ while ( left < right ) {
18
+ const sum = nums [ i ] + nums [ left ] + nums [ right ] ;
19
+
20
+ if ( sum === 0 ) {
21
+ result . push ( [ nums [ i ] , nums [ left ] , nums [ right ] ] ) ;
22
+
23
+ // 중복 미리 제거
24
+ while ( left < right && nums [ left ] === nums [ left + 1 ] ) left ++ ;
25
+ while ( left < right && nums [ right ] === nums [ right - 1 ] ) right -- ;
26
+
27
+ left ++ ;
28
+ right -- ;
29
+ } else if ( sum < 0 ) {
30
+ left ++ ;
31
+ } else {
32
+ right -- ;
33
+ }
34
+ }
35
+ }
36
+
37
+ return result ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments