File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed
Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -6,19 +6,24 @@ var threeSum = function (nums) {
66 const arr = nums . sort ( ( a , b ) => a - b ) ;
77 const ans = [ ] ;
88 const set = new Set ( ) ;
9+
910 const findT = ( t , idx ) => {
10- let start = 0 ;
11- let end = nums . length - 1 ;
11+ let start = idx + 1 ;
12+ let end = arr . length - 1 ;
1213 if ( start === idx ) start ++ ;
1314 if ( end === idx ) end -- ;
15+
1416 while ( start < end ) {
1517 if ( arr [ start ] + arr [ end ] === t ) {
16- const temp = [ arr [ idx ] , arr [ start ] , arr [ end ] ] . sort ( ( a , b ) => a - b ) ;
18+ const temp = [ arr [ idx ] , arr [ start ] , arr [ end ] ] ;
1719 if ( ! set . has ( temp . join ( "" ) ) ) {
1820 ans . push ( temp ) ;
1921 set . add ( temp . join ( "" ) ) ;
2022 }
2123 start ++ ;
24+ end -- ;
25+ while ( start < end && arr [ start ] === arr [ start - 1 ] ) start ++ ;
26+ while ( start < end && arr [ end ] === arr [ end + 1 ] ) end -- ;
2227 } else if ( arr [ start ] + arr [ end ] < t ) {
2328 start ++ ;
2429 } else if ( arr [ start ] + arr [ end ] > t ) {
@@ -28,8 +33,7 @@ var threeSum = function (nums) {
2833 if ( end === idx ) end -- ;
2934 }
3035 } ;
31-
32- for ( let i = 0 ; i < nums . length ; i ++ ) {
36+ for ( let i = 0 ; i < arr . length ; i ++ ) {
3337 if ( arr [ i ] === arr [ i - 1 ] ) continue ;
3438 findT ( arr [ i ] * - 1 , i ) ;
3539 }
You can’t perform that action at this time.
0 commit comments