In the Leetcode 4Sum problem solution in Python programming Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
Leetcode 4Sum problem solution in Python programming
class Solution(object):
def fourSum(self, nums, target):
if nums == None or len(nums) < 4:
return []
nums.sort()
n = len(nums)
res = []
for i in range(n-3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i+1,n-2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
left = j + 1
right = n - 1
while left < right:
sum_ = nums[i] + nums[j] + nums[left] + nums[right]
if sum_ == target:
res.append([nums[i], nums[j], nums[left], nums[right]])
left += 1
right -= 1
while left < right and nums[left] == nums[left - 1]:
left += 1
while left < right and nums[right] == nums[right + 1]:
right -= 1
elif sum_ < target:
left += 1
else:
right -= 1
return res
- Leetcode 4Sum problem solution in C++
- Leetcode 4Sum problem solution in Java
- Leetcode 4Sum problem solution in C
- Leetcode 4Sum problem solution in C#