File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public List <List <Integer >> threeSum (int [] nums ) {
5
+ List <List <Integer >> result = new ArrayList <>();
6
+ Arrays .sort (nums );
7
+ int n = nums .length ;
8
+
9
+ for (int i = 0 ; i < n - 2 ; i ++) {
10
+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
11
+
12
+ int left = i + 1 ;
13
+ int right = n - 1 ;
14
+
15
+ while (left < right ) {
16
+ int sum = nums [i ] + nums [left ] + nums [right ];
17
+
18
+ if (sum == 0 ) {
19
+ result .add (Arrays .asList (nums [i ], nums [left ], nums [right ]));
20
+
21
+ while (left < right && nums [left ] == nums [left + 1 ]) left ++;
22
+ while (left < right && nums [right ] == nums [right - 1 ]) right --;
23
+
24
+ left ++;
25
+ right --;
26
+ } else if (sum < 0 ) {
27
+ left ++;
28
+ } else {
29
+ right --;
30
+ }
31
+ }
32
+ }
33
+
34
+ return result ;
35
+ }
36
+ }
37
+
You can’t perform that action at this time.
0 commit comments