Skip to content

Commit

Permalink
feature: four sum revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
solairerove committed Dec 6, 2023
1 parent 6a31d17 commit 29b8132
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions arrays/FourSum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# O(n^3) time || O(1) space
def four_sum(self, nums: List[int], target: int) -> List[List[int]]:
nums.sort()
quadruplets = []
res = []
for i in range(len(nums) - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
Expand All @@ -15,10 +15,10 @@ def four_sum(self, nums: List[int], target: int) -> List[List[int]]:

low, high = j + 1, len(nums) - 1
while low < high:
sum_ = nums[i] + nums[j] + nums[low] + nums[high]
_sum = nums[i] + nums[j] + nums[low] + nums[high]

if sum_ == target:
quadruplets.append([nums[i], nums[j], nums[low], nums[high]])
if _sum == target:
res.append([nums[i], nums[j], nums[low], nums[high]])
low, high = low + 1, high - 1

while low < high and nums[low] == nums[low - 1]:
Expand All @@ -27,9 +27,9 @@ def four_sum(self, nums: List[int], target: int) -> List[List[int]]:
while low < high and nums[high] == nums[high + 1]:
high -= 1

elif sum_ < target:
elif _sum < target:
low += 1
else:
high -= 1

return quadruplets
return res

0 comments on commit 29b8132

Please sign in to comment.