File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * 시간복잡도 : O(N^2)
3+ * 공간복잡도 : O(1)
4+ * */
5+ import java .util .*;
6+
7+ class Solution {
8+ public List <List <Integer >> threeSum (int [] nums ) {
9+ List <List <Integer >> result = new ArrayList <>();
10+ Arrays .sort (nums );
11+
12+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
13+ if (i > 0 && nums [i ] == nums [i - 1 ]) {
14+ continue ;
15+ }
16+
17+ int left = i + 1 ;
18+ int right = nums .length - 1 ;
19+
20+ while (left < right ) {
21+ int sum = nums [i ] + nums [left ] + nums [right ];
22+
23+ if (sum == 0 )
24+ {
25+ // 합이 0인 경우 결과에 추가
26+ result .add (Arrays .asList (nums [i ], nums [left ], nums [right ]));
27+
28+ // 중복된 값 건너뛰기
29+ while (left < right && nums [left ] == nums [left + 1 ])
30+ left ++;
31+
32+ while (left < right && nums [right ] == nums [right - 1 ])
33+ right --;
34+
35+ left ++;
36+ right --;
37+ }
38+ else if (sum < 0 )
39+ left ++;
40+ else
41+ right --;
42+
43+ }
44+ }
45+ return result ;
46+ }
47+ }
You can’t perform that action at this time.
0 commit comments