File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -110,3 +110,37 @@ function threeSum(nums: number[]): number[][] {
110110
111111๊ทผ๋ฐ ์ด๋ป๊ฒ๋ ๋ชป๋ง๋ฆ.
112112 */
113+
114+ /**
115+ ์ด์ ํด๋ต์ ๋ณด์์ผ๋, ๋ค์ํ๋ฒ ํ์ด๋ณด์.
116+ ํต์ฌ์ ํฌ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ, ์๋ฃ๊ตฌ์กฐ์ ์ฝ๋ฉ์ด์ง ์๋ ๊ฒ
117+ */
118+
119+ function threeSum ( nums : number [ ] ) : number [ ] [ ] {
120+ nums . sort ( ( a , b ) => a - b ) ;
121+ const results = [ ] ;
122+
123+ // [-4, -1, -1, 0, 1, 2]
124+ for ( let i = 0 ; i < nums . length ; i ++ ) {
125+ while ( nums [ i - 1 ] === nums [ i ] ) i ++ ;
126+
127+ let low = i + 1 ;
128+ let high = nums . length - 1 ;
129+ while ( low < high ) {
130+ const sum = nums [ i ] + nums [ low ] + nums [ high ] ;
131+ if ( sum < 0 ) {
132+ low ++ ;
133+ } else if ( sum > 0 ) {
134+ high -- ;
135+ } else {
136+ while ( nums [ high ] === nums [ high - 1 ] ) high -- ;
137+ while ( nums [ low ] === nums [ low + 1 ] ) low ++ ;
138+ results . push ( [ nums [ i ] , nums [ low ] , nums [ high ] ] ) ;
139+ low ++ ;
140+ high -- ;
141+ }
142+ }
143+ }
144+
145+ return results ;
146+ }
You canโt perform that action at this time.
0 commit comments