# Question

Formatted question description: https://leetcode.ca/all/15.html

```
15 - 3 Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
@tag-array
```

# Algorithm

Sort the original array, and then start traversing the sorted array.

Note that the traversal is not to the last stop, but to the third from the bottom. Here we can do a pruning optimization first, that is, break when traversing to a positive number, Because the array is now ordered, if the first number to be fixed is a positive number, then the following numbers if they are all positive numbers, the sum will never be zero.

Then add the process of skipping if repeated. The processing method is to start with the second number. If it is equal to the previous number, skip it because you don’t want to fix the same number twice.

For the traversed number, subtract the fix number from 0 to get a target, and then only need to find the sum of the two numbers is equal to the target. Use two pointers to point to the first and last two numbers of the array starting after the fix number.

- If the sum of the two numbers happens to be the target, then the two numbers and the fix number are stored in the result together. Then is to skip the step of repeating numbers. Both pointers need to detect repeated numbers.
- If the sum of the two numbers is less than the target, move the pointer i on the left one bit to the right to increase the number pointed to.
- In the same way, if the sum of the two numbers is greater than the target, move the pointer j on the right to the left to reduce the number pointed to

# Code

Java