File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 시간 복잡도: O(n^2)
3
+ * 공간 복잡도: O(n)
4
+ */
5
+ class Solution {
6
+ public List <List <Integer >> threeSum (int [] nums ) {
7
+ int left , right , sum ;
8
+ List <List <Integer >> results = new ArrayList <>();
9
+ Arrays .sort (nums );
10
+
11
+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
12
+ if (i > 0 && nums [i ] == nums [i - 1 ]) {
13
+ continue ;
14
+ }
15
+
16
+ left = i + 1 ;
17
+ right = nums .length - 1 ;
18
+
19
+ while (left < right ) {
20
+ sum = nums [i ] + nums [left ] + nums [right ];
21
+
22
+ if (sum < 0 ) {
23
+ left ++;
24
+ } else if (sum > 0 ) {
25
+ right --;
26
+ } else {
27
+ results .add (Arrays .asList (nums [i ], nums [left ], nums [right ]));
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
+ }
39
+ }
40
+ return results ;
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments