-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sum3.java
53 lines (48 loc) · 1.57 KB
/
Sum3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
public class Sum3 {
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
public static ArrayList<ArrayList<Integer>> threeSum(int[] numbers) {
// write your code here
int sum;
int len = numbers.length;
ArrayList<ArrayList<Integer>> L2 = new ArrayList<ArrayList<Integer>>();
Arrays.sort(numbers);
for(int i = 0;i < len;i++){
int j = i + 1;
int k = len - 1;
while(j < k){
sum = numbers[i] + numbers[j] + numbers[k];
if(sum == 0){
System.out.println("dsadsa");
ArrayList<Integer> L1 = new ArrayList<Integer>();
L1.add(numbers[i]);
L1.add(numbers[j]);
L1.add(numbers[k]);
if(!L2.contains(L1)){
System.out.println(L1);
L2.add(L1);
System.out.println(L2);
}
//L1.clear();
System.out.println(L2);
j++;
k--;
}
else if(sum > 0){
k--;
}
else{
j++;
}
}
}
return L2;
}
public static void main(String args[]){
int[] A = {-1,0,1};
threeSum(A);
}
}