nums
.res
to store the quadruplets.i
from 0
to len(nums) - 3
:
i
is greater than 0
and nums[i]
is the same as nums[i - 1]
, skip to avoid duplicates.j
from i + 1
to len(nums) - 2
:
j
is greater than i + 1
and nums[j]
is the same as nums[j - 1]
, skip to avoid duplicates.left = j + 1
and right = len(nums) - 1
.left < right
:
s = nums[i] + nums[j] + nums[left] + nums[right]
.s
is less than target
, increment left
.s
is greater than target
, decrement right
.s
equals target
, add [nums[i], nums[j], nums[left], nums[right]]
to res
, then:
left
while nums[left]
is the same as nums[left - 1]
.right
while nums[right]
is the same as nums[right + 1]
.res
containing all unique quadruplets.