Skip to content

Commit c1d27d4

Browse files
authored
1 parent 115a631 commit c1d27d4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

#15/java/Solution.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)