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
1
+ /**
2
+ * https://leetcode.com/problems/3sum/
3
+ * Your runtime beats 33.97 % of java submissions.
4
+ * Your memory usage beats 24.76 % of java submissions.
5
+ */
6
+ class Solution {
7
+ public List <List <Integer >> threeSum (int [] nums ) {
8
+ List <List <Integer >> ll = new LinkedList <>();
9
+ if (nums .length <3 ) return ll ;
10
+ Arrays .sort (nums );
11
+ Set <Pair <Integer , Integer >> s = new HashSet <>();
12
+ for (int i =0 ; i <nums .length -2 ; i ++){
13
+ int j =i +1 , k =nums .length -1 ;
14
+ while (j <k ){
15
+ int sum = nums [i ]+nums [j ]+nums [k ];
16
+ if (sum ==0 ){
17
+ int min = Math .min (nums [i ], Math .min (nums [j ], nums [k ]));
18
+ int max = Math .max (nums [i ], Math .max (nums [j ], nums [k ]));
19
+ if (s .add (new Pair <Integer , Integer >(min , max ))){
20
+ List <Integer > l = new LinkedList <>();
21
+ l .add (nums [i ]);
22
+ l .add (nums [j ]);
23
+ l .add (nums [k ]);
24
+ ll .add (l );
25
+ }
26
+ j ++;
27
+ k --;
28
+ } else if (sum <0 ) j ++;
29
+ else k --;
30
+ }
31
+ }
32
+ return ll ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments