public class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> threeSum(int[] nums) { if(nums.length < 3 || nums == null) return res; Arrays.sort(nums); int len = nums.length; for (int i = 0; i < len; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; find(nums, i+1, len-1, nums[i]); } return res; } public void find(int []nums, int st, int end, int tar) { while(st < end) { if(nums[st]+nums[end]+tar == 0) { List<Integer> ans = new ArrayList<Integer>(); ans.add(tar); ans.add(nums[st]); ans.add(nums[end]); res.add(ans); while(st < end && nums[st] == nums[st+1]) st++; while(st < end && nums[end] == nums[end-1]) end--; st++; end--; } else if(nums[st] + nums[end] + tar < 0) st++; else end--; } } }