1 public class Solution { 2 public ArrayList<ArrayList<Integer>> threeSum(int[] num) { 3 if(num == null) 4 return null; 5 6 Arrays.sort(num); 7 HashSet<ArrayList<Integer>> set = new HashSet<ArrayList<Integer>>(); 8 9 for(int i = 0; i < num.length; i++){ 10 int start = i+1; 11 int end = num.length-1; 12 13 while(start < end){ 14 int sum = num[i] + num[start] + num[end]; 15 if(sum == 0){ 16 ArrayList<Integer> tmp = new ArrayList<Integer>(); 17 tmp.add(num[i]); 18 tmp.add(num[start]); 19 tmp.add(num[end]); 20 set.add(tmp); 21 start++; 22 end--; 23 } 24 else if(sum < 0) 25 start++; 26 else 27 end--; 28 } 29 } 30 31 ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); 32 result.addAll(set); 33 return result; 34 35 } 36 }