File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .Arrays ;
3+ import java .util .List ;
4+
5+ public class kimjunyoung90 {
6+ public List <List <Integer >> threeSum (int [] nums ) {
7+ List <List <Integer >> answers = new ArrayList <>();
8+ //์ซ์๋ค์ ๋ฏธ๋ฆฌ ์ ๋ ฌํด์ 3๋ฒ์งธ ๋จ๊ณ์์ ์ถ๊ฐ์ ์ธ ์ ๋ ฌ์ ์๊ฒ ๋ง๋ค์..
9+ Arrays .sort (nums );
10+
11+ // 1. ์์๊ฐ ์ค๋ณต๋์ง ์๋ 3๊ฐ์ ์ซ์ ์กฐํฉ์ ์ฐพ์
12+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
13+
14+ //i ๊ฐ์ด ์ ์์๋ ๊ฐ์ ๊ฒฝ์ฐ ํ์ ๊ฑด๋ ๋๊ธฐ
15+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
16+
17+ //two pointer ์ ์ฉ
18+ int left = i + 1 ;
19+ int right = nums .length - 1 ;
20+
21+ while (left < right ) {
22+ int sum = nums [i ] + nums [left ] + nums [right ];
23+ if (sum == 0 ) {
24+ answers .add (Arrays .asList (nums [i ], nums [left ], nums [right ]));
25+
26+ //left ์ค๋ณต ์ฒดํฌ
27+ while (left < right && nums [left ] == nums [left + 1 ]) left ++;
28+
29+ //right ์ค๋ณต ์ฒดํฌ
30+ while (left < right && nums [right ] == nums [right - 1 ]) right --;
31+
32+ left ++;
33+ right --;
34+ } else if (sum < 0 ) {
35+ left ++;
36+ } else {
37+ right --;
38+ }
39+ }
40+ }
41+
42+ return answers ;
43+ }
44+ }
You canโt perform that action at this time.
0 commit comments