```
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
int length = nums.length;
if (length < 3) {
return result;
}
Arrays.sort(nums);
for (int i = 0; i < length - 2; i++) {
if (nums[i] > 0) {
return result;
}
if (i == 0 || nums[i] != nums[i - 1]) {
int target = -nums[i];
int low = i + 1, high = length - 1;
while (low < high) {
if (nums[high] < 0 || nums[low] > target) {
break;
}
if (nums[low] + nums[high] == target) {
result.add(Arrays.asList(nums[i], nums[low], nums[high]));
while(++low<high && nums[low] == nums[low-1]);
while(low<--high && nums[high] == nums[high+1]);
} else if (nums[low] + nums[high] < target) {
while(++low<high && nums[low] == nums[low-1]);
} else {
while(low<--high && nums[high] == nums[high+1]);
}
}
}
}
return result;
}
```

}