We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 415e9e9 commit 6779b7fCopy full SHA for 6779b7f
3sum/mintheon.java
@@ -0,0 +1,35 @@
1
+import java.util.ArrayList;
2
+import java.util.Arrays;
3
+import java.util.HashSet;
4
+import java.util.List;
5
+import java.util.Set;
6
+
7
+class Solution {
8
+ public List<List<Integer>> threeSum(int[] nums) {
9
+ Arrays.sort(nums);
10
11
+ Set<List<Integer>> answer = new HashSet<>();
12
13
+ for(int i = 0; i < nums.length - 2; i++) {
14
+ int left = i + 1;
15
+ int right = nums.length - 1;
16
17
+ while(left < right) {
18
+ int sum = nums[i] + nums[left] + nums[right];
19
20
+ if(sum < 0) {
21
+ left++;
22
+ } else if(sum > 0) {
23
+ right--;
24
+ } else{
25
+ answer.add(List.of(nums[i], nums[left], nums[right]));
26
27
28
29
+ }
30
31
32
33
+ return new ArrayList<>(answer);
34
35
+}
0 commit comments