Generate unique triplets14.02
Unique by value: after sorting, skip duplicate a[i], a[j], a[k] so [1, 1, 2] and another [1, 1, 2] do not both appear.
Fix i, skip equal i-1. Then two-pointer j,k skipping equal values after a hit.
Same skill, three traces. Read all three before you try it.
01sorted skip - given
a = [1, 1, 2, 2, 3],t = 6- walk
1,2,3is the unique value triplet- result
- [
[1, 2, 3]]
02all distinct - given
a = [0, 1, 2, 3],t = 6- walk
0,3,3invalid1,2,3works0,2,4no- result
- [
[1, 2, 3]]
03all same - given
a = [2, 2, 2, 2],t = 6- walk
only
{2, 2, 2}- result
- [
[2, 2, 2]]
uniqueTriplets(a, t).a is a sorted array of integers. t is the target sum.Return unique value triplets that sum to
t. Do not repeat the same three values.On
[1, 1, 2, 2, 3] target 6 → [[1, 2, 3]]. On [2, 2, 2, 2] target 6 → [[2, 2, 2]].constraintsa has length ≥ 3. values and t are integers.
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.